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 trialAndrea Molnar
13,459 PointsI got to the same result through a slightly different approach. Is my code still correct?
My code is:
var minNumber = parseInt(prompt("Please give me a minimum number: "));
var maxNumber = parseInt(prompt("Please give me a maximum number: "));
randomNumber = parseInt(Math.random() * (maxNumber - minNumber) + minNumber);
document.write("Your number is: " + randomNumber);
Samuel Trejo
4,563 PointsSamuel Trejo
4,563 PointsNot quite, because the last number will never get generated.
For example let's say range is 5 to 10.
parseInt(Math.random() * (10 - 5) + 5), Math.random() * 5 will give you a range between 0.000... and 4.999..., add 5 and it will give you a range between 5.000... and 9.999..., parseInt() will then return a range of 5, 6, 7, 8, and 9, but never 10
This is why we do + 1 and round down.
Math.floor(Math.random() * (10 - 5 + 1) + 5), Math.random() * 6 will give you a range between 0.000... and 5.999..., add 5 and it will give you a range between 5.000... and 10.999..., Math.floor will round it down and return a range of 5, 6, 7, 8, 9, and 10
I believe "parseInt(Math.random() * (10 - 5 + 1) + 5)" will technically work but it's purpose is not to remove decimals. It's purpose is to convert a string into a number.