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

Vaughn Council
Vaughn Council
2,419 Points

Math.random

When generating a random number between bottomNumber and topNumber would it also be possible to write the code as follows?

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

Just wondering if Javascript will also allow for everything to be kept within parentheses?

1 Answer

JavaScript will allow you to use parentheses like that, however, with the JS you have written you will get a number between 0 and topNumber.

Since you have added and subtracted bottomNumber in that expression, it is cancelled out and has no impact on the sum.