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 Build a Random Number Guessing Game

Bruno Dias
Bruno Dias
10,554 Points

Hi, One thing that is not very clear for me yet is why do we need to add the +1 at the end of randomNumber variable?

+1 at the end of randomNumber variable

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Well, because in the lecture, he said "we want number between 0 - 6".

Math.random() will generate random number in range between 0 and 1. Notice it excluded 1, but included 0. Math.floor(), you can think of this method as to convert a floating point number into integer by discarding its decimal part (for positive number). Therefore Math.floor(Math.random()) will only give you number between 0-5, never gets you 6. That's why you need the +1.

Bruno Dias
Bruno Dias
10,554 Points

Oh I see! That makes sense. Thank you. All clear now =)