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
Bianca Rocha
1,800 PointsI can't seem to get the random number between the user input numbers?
I can enter in the user numbers fine but when the randomly generated number appears it's not between the numbers inputted by the user, can anybody tell me what I've done wrong here :(
This is my code:
var userNumberBottom = prompt("Please enter a starting number");
var userNumberTop = prompt("Please enter an ending number");
parseInt(userNumberBottom);
parseInt(userNumberTop);
randomNumber = Math.floor(Math.random() * (userNumberTop - userNumberBottom + 1)) + userNumberBottom;
document.write("<h2>This is your randomly generated number: " + randomNumber + "<br>[Between " + userNumberBottom + " and " + userNumberTop +"]</br></h2>");
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Bianca,
You've called parseInt on your user input but you didn't save the return value in a variable. So when you get to your random formula, those variables are still storing the user input as a string.
One way to solve it is to store the return value back into the same variable like this:
userNumberBottom = parseInt(userNumberBottom);
So now when you reach the random formula, your userNumberBottom variable will be storing the user input as an integer and not a string anymore.
Bianca Rocha
1,800 PointsSilly me - I understand completely! Thanks a million Jason Anello - it works perfectly now!! :)
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsAlso, you could combine these 2 steps into one if you like and write it like this:
var userNumberBottom = parseInt(prompt("Please enter a starting number"));The prompt call will return the user input and it will get passed directly into the parseInt method and the return integer from that will get stored in that variable.
You could do that for both of your inputs and then go straight to your random formula.