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

Shan Liu
Shan Liu
3,612 Points

Concerning the min/max number generator

I don't understand why it is: Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + 1;

I used the following: var userNum1 = prompt('provide the min number');
var minNum = parseInt(userNum1);
var userNum2 = prompt('provide the max number');
var maxNum = parseInt(userNum2);

var producedNum = Math.floor(Math.random() * maxNum) + minNum;

var message = producedNum + ' is a number between ' + minNum + ' and ' + maxNum;

document.write(message);

and it worked, or was my code written incorrectly?

Shan Liu
Shan Liu
3,612 Points

sorry, i also don't understand that when printing the message, why do we need to use ' + xxxxx + ', the plus signs on either side, when we can just write the var name directly?

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Shan,

The objective of this random number generator is to produce a number between a given range. The problem with the way you wrote it is that it could potentially produce a number greater than your max number.

For example, say you wanted to produce a random number between 5 and 10, if you plug them into your formula you will have the following:

= Math.floor(Math.random() * maxNum) + minNum
= Math.floor(Math.random() * 10) + 5

Math.random() will generate a random number between 0 (inclusive) and 1 (exclusive).

Now lets say that your Math.random() produced 0.95.

= Math.floor(0.95 * 10) + 5
= Math.floor(9.5) + 5
= 9 + 5
= 14

So now you have exceeded your maxNum.

The way it is shown in the video will keep it within the range you specify. Note that you have a typo in the way you wrote it, you should be adding the bottomNumber to the end, not 1.

Assuming the same values as before:

= Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber
= Math.floor(Math.random() * (10 - 5 +1)) + 5
= Math.floor(0.95 * 6) + 5
= Math.floor(5.7) + 5
= 5 + 5
= 10

So you can see that the highest possible number that will be generated is 10 in this case, which is what we want.

The same can be demonstrated for the minimum number.

Regarding your question on the plus signs, the goal is to concatenate a string that incorporates the variables. You will need to add things together in order to accomplish this. They won't automatically insert themselves.

Hope this helps.

Shan Liu
Shan Liu
3,612 Points

thank you so much for the in-depth explanation.