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 trialJim David
Full Stack JavaScript Techdegree Graduate 16,887 PointsRandom number results to NaN Why does my browser not recognize some random numbers?
I am trying to post my results on the console and the HTML, but I keep receiving "NaN is a random number between 1 and NaN." as a result. I copied the solution exactly the way it is, but I keep getting this issue
2 Answers
Jim David
Full Stack JavaScript Techdegree Graduate 16,887 PointsHere is my code
// Collect input from a user
const inputHigh = prompt('Please provide a number');
// Convert the input to a number
const highNumber = parseInt(inputHigh);
// Use Math.random() and the user's number to generate a random number
const randomNumber = Math.floor( Math.random() * highNumber ) + 1;
// Create a message displaying the random number
document.write(${randomNumber} is a random number between 1 and ${highNumber}.
);
Dustin Wildes
12,094 PointsI tested it out and I did not see a problem (assuming your 'document.write' has the appropriate formatting.). Give me an example of a number that does not work? If you can't, try inputting "parseFloat(inputHigh);" instead. And not to insult you, but make sure you are not hitting the cancel button by mistake.
Billy Chui
6,852 Pointsi think you miss the open and closing backquote (`) on your document.write section.
Dustin Wildes
12,094 PointsGiven the way it's formatted in his comment, I don't think he forgot it. Plus it would prevent the program from running altogether.
Robert Darby
9,360 PointsRobert Darby
9,360 PointsWithout seeing your code makes it very hard to help but I would double check you're storing the numbers as integers in the variables and not string.
NaN stands for "Not a Number"
Remember:
1 is an integer/number but "1" is a string. A string will be NaN when your code is expecting integer.