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

JavaScript JavaScript Basics (Retired) Working With Numbers The Random Challenge Solution

My answer is different, but does it provide the same result?

var firstInput = prompt("Please enter a low number");
var secondInput = prompt("Please enter a high number");

var lowNum = parseInt(firstInput); var highNum = parseInt(secondInput);

var randomNumber = Math.floor(Math.random(lowNum - 1) * highNum) + 1;

document.write("Your random number is " + randomNumber);

1 Answer

Steven Parker
Steven Parker
229,732 Points

The computation of the random number is not quite correct.

Math.random does not take an argument, so the "lownum + 1" will be ignored. This formula will generate a random number from 1 to the value entered for "highNum" no matter what is entered for "lowNum". So the number may be significantly lower than "lowNum".

The formula shown in the video will always generate a number that is no lower than "lowNum", and no higher than "highNum".

Ahhh ok, thanks!