Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,867 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 =)