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

brandonlind2
brandonlind2
7,823 Points

Could someone explain the ( ) in javascript?

Could someone explain this ( ) in javascript not as in functions but as in for exmaple this random number function

function randNum(topNum,BottomNum);
var randomNmuber= Math.floor(Math.random() *
 (topNum- bottomNum)//< what are these brakets?
) +bottomNumber;

1 Answer

Bryan Peters
Bryan Peters
11,996 Points

Those are used for mathematical order of operations.

In this case, calculate the difference between top num and bottom num to have the range value BEFORE multiplying by the random number.

So, if math.random is 0.5, top num is 10, and bottom num is 1:

0.5 * (10-1) = 4.5 instead of 0.5 * 10 - 1 = 4