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

Immanuel Jaeggi
Immanuel Jaeggi
5,164 Points

second part of challenge needs some explanation

Could you help me out with this part of the second challenge?

var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;

Im trying to figure out how the lastbottomNumber` affects the statement. He mentions some vague numbers in the video that he says not to worry about, but I would like to know exactly what is going on here i.e. why he needs to add the bottomNumber at the end.

Thanks!

3 Answers

Antti Lylander
Antti Lylander
9,686 Points
var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;

Lets try and put some real numbers in the equation:

Math.random = 0.9
bottomNumber = 6
topNumber = 9

-> Math.floor(0.9 * (9 - 6 + 1)) + 6
-> Math.floor(3.6) + 6
-> 3 + 6
-> 9

Now, try it with different values, with and without the last occurrence of bottomNumber. It is there because that is the mimimum number you want to get out of the equation. Without it, your randomNumber can be 0.

var survivingNumber = Math.floor(Math.random() * (maxNumber - minimumNumber + 1)) + minimumNumber;

Sooo ehhh minimumNumber is like the lowest accepted result possible like when you buy 20 cans of beer and some of your friends is still missing and when you just want to start the drinking message comes to phone from friends they ask you to leave at least 6 cans of beer and I look at the message and think dam no less than 6 or I am doomed. So I take 6 cans away from the start - minimumNumber(6) and start drinking with friends but the catch is that before we start drinking we just empty cans to fill our huge cups but with shaky hands :( we spill a very little amount on the floor so yeah we always buy 1 extra to fill missing parts by time other friends come we can drink any amount of cups of beer yea because we don't know when they are coming it becomes random but no matter how good of drinkers we are we know there will always be 6 cans hiden and when friends there will always be + minimumNumber(6) hiden for them.

And yea I know there are few logical mistakes in there but this one should let even alcoholic to understand whats going on here. It would take too much of time for me to think of a perfect explanation so spare me. - Shingaki

Victor Adeshile
Victor Adeshile
1,221 Points
// Create a variable to store the 1st and 2nd inputs.
var ask1 = parseInt(prompt('Enter the first number: '))
var ask2 = parseInt(prompt('Enter the Second number: '))

// create a Math.random function to produce a random number from first to second inputs.
var totR = Math.ceil(Math.random() * (ask1 - ask2) + ask2)
// What is happening above is, if the random number of ask1  and ask 2 is evaluated and is 1 for example, it would always // be lower than the two entered values. So, to keep the value in between the two inputs, we add up the second number.
// thus the reason for the + ask2 in the function above.

// print the output to the screen.
document.write(` The random number generated between ${ask1} and ${ask2} is ${totR}`)

Hope this helps.

pat barosy
pat barosy
6,759 Points

Why the bottom number in particular? could we do the same with the top number?