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 trial
Asfand Yar
2,453 Pointswhy i am getting nan for middle variable
var input1 = prompt("please input the 2nd number "); var bottomNumber = parseInt(input1); var randomNumber1 = Math.floor(Math.random() * bottomNumber)+1; var message1 = "<p> this is the 2nd input from user " + input1 + " this is the 2nd random number " + randomNumber1 + "</p>."; var middle; document.write(message1);
var middle = (randomNumber1 / randomNumber); document.write(middle);
var input = prompt ("please enter the number "); var topNumber = parseInt(input); var randomNumber = Math.floor(Math.random() * topNumber )+1 ; var message = "<p> this is the input from user " + input + " this is the random number " + randomNumber + "</p>."; var middle; document.write(message);
2 Answers
Matt Brock
28,330 PointsHey Asfand, looks like you're calling randomNumber before you define it. Your code seems to be a little out of order as well, such as asking the user for the 2nd number first. I'm not quite sure what you're trying to do with the code, but here is a solution that works and is cleaned up a bit for clarity:
var input1 = prompt("please input the 2nd number");
var bottomNumber = parseInt(input1);
var randomNumber1 = Math.floor(Math.random() * bottomNumber) + 1;
var message1 = "<p> this is the 2nd input from user " + input1 + " this is the 2nd random number " + randomNumber1 + "</p>";
// Output `message1`
document.write(message1);
// Assign `randomNumber`
var randomNumber = Math.floor(Math.random() * topNumber ) + 1;
// Assign `middle`
var middle = (randomNumber1 / randomNumber);
// Output `middle`
document.write(middle);
var input = prompt ("please enter the number");
var topNumber = parseInt(input);
var message = "<p>this is the 1st input from user " + input + " this is the random number " + randomNumber + "</p>";
// Output `message`
document.write(message);
Also, you can wrap your code in three tick marks "```" to format it in your question.
Asfand Yar
2,453 Pointsthanks Matt i got it now
Matt Brock
28,330 PointsThat's great! Glad to help.