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 Numbers The Math Object Random Number Challenge – Two Numbers Solution

Really not understand the math euqation of lownumber and highnumber. which is (highnumber - lownumber + 1);

I am trying to figure out this. Why we put this math formula in code.

1 Answer

Tobias Kloy
Tobias Kloy
3,655 Points

The Math.random() function creates a number between 0 and 1 while 0 is included but 1 not.

Let's say:

var lowNumber = 2;
var highNumber = 8;
var randomNumber = 0.17237478506267383;

If you now take randomNumber * (highNumber - lowNumber) you would receive a number below 1 and with Match.floor() it would turn to 0. That is why we add 1. The lower and higher numbers are just for fun, we could also use a single number instead.

boi
boi
14,241 Points

This is not a good answer.