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
charles bempah
1,295 PointsFormula looks right but random number generated isn't right. Someone please explain.
1 Answer
KRIS NIKOLAISEN
54,974 PointsThe proper random formula is:
Math.floor(Math.random() * (max - min + 1) ) + min;
Using your variables:
Math.floor(Math.random() * (userNumber2 - userNumber + 1) ) + userNumber
they are reversed.
parseInt on it's own won't update userNumber. The result needs to be assigned:
var userNumber = prompt(`Please type in a random number`);
userNumber = parseInt(userNumber);
var userNumber2 = prompt(`Please type in a second random number`);
userNumber2 = parseInt(userNumber2);
Otherwise:
Math.floor(Math.random() * (userNumber2 - userNumber + 1) )
generates a number. But the following is concatenated
+ userNumber