Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Aman Rai
Aman Rai
3,040 Points

I got the same result but with a different approach. Is my code still correct?

var selectNum = prompt('Insert a number');
var selectedNum = parseInt(selectNum);
var selectNum2 = prompt('Insert another number');
var selectedNum2 = parseInt(selectNum2);
var yourRanNum = Math.floor(Math.random() * selectedNum2) + selectedNum;
var message = '<p>' + yourRanNum + ' is a number between ' + selectedNum + ' and ' + selectedNum2 + '</p>';
document.write(message);

3 Answers

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,030 Points

Dear Aman Rai, I checked your code you are doing great job. One question can have many approaches to solve if your answer is satisfied by the terms required then it is perfect. In software engineering we have a term called KISS- It means keep it simple. If it works it is your best method.

Enjoy Learning

Chevy McMartin
Chevy McMartin
9,680 Points

Hi, your code won't work properly. Try your code using some high numbers like from 100 to 110.

It's just the math logic.

Willemijn B
Willemijn B
5,577 Points

I did the same thing and I can't seem to figure out the math that makes high numbers not work properly with this approach (whereas it does work with the solution given). Can someone explain?

This is the problem line:

var yourRanNum = Math.floor( Math.random() * selectedNum2 ) + selectedNum;

Consider this element in isolation:

Math.random() * selectedNum2

In this fragment, a random value between 0 and 1 [Math.random()] is multiplied by the second value entered by the user [selectedNum2]. Let's say that selectedNum2 is 10. This snippet of code will therefore generate a value between 0 and 9.99999999 (we won't get 10 because the Math.random() function never returns 1).

Now apply the Math.floor() function...

Math.floor( Math.random() * selectedNum2 )

Math.floor() rounds down the given value, so from this fragment we attain an integer between 0 and 9. To which we then add the first number that the user entered [selectedNum]...

Math.floor( Math.random() * selectedNum2 ) + selectedNum

Now say that selectedNum is 5. We therefore have a range of possible outputs for this variable between (0 + 5) and (9 + 5), i.e. 5 and 14. But the goal of the code is to return a value between 5 and 10.

That's why we have to calculate the difference between the two submitted user values, i.e. 10 - 5 = 5. We want to generate a random value in that range (i.e. between 0 and 5) and add it to the lower of the two given user values, giving us a range of potential outputs between (5 + 0) and (5 + 5) -- i.e. a number between 5 and 10.

We can do this as follows:

var difference = selectedNum2 - selectedNum;
var yourRanNum = Math.floor( Math.random() * (difference + 1) ) + selectedNum;

Note that we have to multiply by (difference + 1) in order to attain a range that includes the upper as well as the lower value provided by the user. Without the +1, we'd be limited (continuing the example above) to a range between 5 and 9.