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

Need Clarification, What does +1 do at the end of: var randomNumber = Math.floor( Math.random() * 6 ) + 1;

I understand most of this just can't seem to remember what the +1 does at the end. Thanks!

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The +1 alters both the upper and lower bounds And what I mean by this is that it ensures that the lowest value will be at least 1 and the upper bound will be 6. When you do Math.random() it will generate a number between 0 and 1. That includes 0 but doesn't include 1. For example it could roll a 0 on the first call and a .99 on the second.

And let's take a look at both those examples. If it were to return a 0 and then you multiply that by 6 and then you floor it, the result would be 0. But if you got a .99 and multiplied it by 6 that would give 5.94. And the floor of that would be 5. So without the +1 the random range would result in a number between 0 and 5.

To make the range instead 1 to 6 instead of 0 to 5, we add the +1. Hope this helps! :sparkles:

Thanks!