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) Making Decisions with Conditional Statements Improving the Random Number Guessing Game

why does he put plus 1

var randomNumber=math.floor(math.random() * 10) + 1; the plus one in the end??

2 Answers

Sreng Hong
Sreng Hong
15,083 Points

Hi Alaa,

In his video, he wants the random number from 1 to 6 and he uses math.floor(math.random() * 6) + 1. So if he doesn't plus 1 at the end, there isn't a chance of getting number 6 from that method. You can try it by yourself again. Good luck.

Sreng

Also, another tidbit of information, adding the +1 to the end of this command increases the minimum and maximum given numbers by 1. The range before adding one is [0,5] (inclusive) and after adding 1 it becomes [1,6] (inclusive). Any number you add this way is going to increase the minimum and maximum possible values by that number.

guillaume minero
guillaume minero
1,629 Points

var randomNumber=math.floor(math.random() * 10) + 1; the plus one in the end??

math.random() * 10 generates a random number in between 0-9 (9.9999999 10 not inclusive)

math.floor rounds the random number downwards

and since we want a number in between 1-10 we add +1 in order to round down in between 1.something and 10.someting