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 trialAndrew Denson
4,544 PointsQuestion on the Random Challenge code
var newNumber = Math.floor(Math.random() * (randomNumberTwo - randomNumberOne + 1)) + randomNumberOne;
My questions is why do you need the parenthesis around this (randomNumberTwo - randomNumberOne + 1)) for it to work properly. I know you need it for it to work, just looking for explanation on the purpose it serves.
Thanks
2 Answers
Jackson Monk
4,527 PointsYou are just using basic math order of operations, telling the interpreter that within the set of innermost parentheses is what you what to do first
Mike Hatch
14,940 PointsThe official way of describing it is called Operator precedence. Scroll down to the Table area. It will show you the order of precedence for multiplication, addition, subtraction, etc.
A simple example:
let a = (1 + 2) * 3)
a // 9
// This is parsed from left to right because the parentheses take higher precedence.
If you instead typed:
let a = 1 + 2 * 3
a // 7
// This is parsed from right to left because multiplication takes higher precedence.