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 trialLorena Maldonado
6,563 PointsIs this a correct answer?
var firstNumber = parseInt (prompt("Type your first number here:")); var secondNumber = parseInt (prompt ("Type your second Number here"));
var result = Math.floor (Math.random () * secondNumber) + firstNumber ;
document.write (result);
1 Answer
Marcus Parsons
15,719 PointsHey Lorena Maldonado,
What you want to happen is have a number that can be within the range of the firstNumber and secondNumber. But, with your code, it can go far beyond that range. The math statement you should be using is this:
var result = Math.floor (Math.random () * (secondNumber - firstNumber + 1)) + firstNumber ;
Now, when you run the script, it will always generate a number that is within the range provided, inclusive of the first number and the second number. If you have any questions about the math in the above statement, I'm happy to help with that!
Dominik Sikiric
2,059 PointsDominik Sikiric
2,059 Pointsso If my first number is for example 2 and second one is 6 and math.random turns to be 0.9 I will get= 0.9*6 = 5.4 = 5 (because of math.floor) + firstNumber(2) = 5+2=7