Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGene Smith
11,076 PointsIssue 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;
}else{
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}.`);
}else{
alert('You need to provide two number. Please try again.');
}
1 Answer
Martin Sole
82,199 PointsHi 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,