JavaScript JavaScript Numbers The Math Object Random Number Challenge – Two Numbers Solution

Gene Smith
Gene Smith
2,362 Points

Issue with alert statement. lowNumber not defined.

I keep getting an error with the alert line below. alert(Your random number between ${lowNumber} and ${highNumber} is ${generatedNumber}.);

Keeps saying the variables aren't defined. Full code is below, please let me know what's messing me up.

// Collect input from a user const firstenteredNumber = prompt("Please provide a number."); const secondenteredNumber = prompt ("Please provide a second number.");

// Convert the input to a number const firstNumber = parseInt(firstenteredNumber); const secondNumber = parseInt(secondenteredNumber);

if(firstNumber && secondNumber){

//Define the high and low numbers  

if(firstNumber > secondNumber){ const highNumber = firstNumber; const lowNumber = secondNumber;

 const highNumber = secondNumber;
 const lowNumber = firstNumber;


// Use Math.random() and the user's number to generate a random number
const generatedNumber = Math.floor(Math.random() * (highNumber-lowNumber + 1) + lowNumber;

// Create a message displaying the random number
alert(`Your random number between ${lowNumber} and ${highNumber} is ${generatedNumber}.`);


  alert('You need to provide two number. Please try again.');


1 Answer

Martin Sole
Martin Sole
80,279 Points

Hi It looks like you're assigning lowNumber and highNumber inside the nested if statement, but the alert message is outside of this. As const(and let) uses block scoping the variables are not accessible outside of the if statement, so your alert message cannot reach them. You would need to declare them outside of the nested if statement,