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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops The Refactor Challenge

Math.floor(Math.random() * 256 We wont be getting 256 with this line, will we?

since 1 is exclusive from Math.random(). Are we eliminating the possibility of getting 256?

kavumo mfinanga
kavumo mfinanga
17,182 Points

Yes, But if you want to get 256, That means 255 will be your maximum random number generated which is the highest decimal value for color (255)

2 Answers

Yes, that's correct. Math.random() doesn't include 1, so we wouldn't get 256.

Hi Wuen Han Lee,

You are right. We won't get 256. There are two reasons for this:

1) As Bryan Laraway stated already, Math.random() does not include 1

2) The Math.floor() function will round down a number to the lower integer. For example, let's take the number 1.6 so Math.floor(1.6) = 1. So since this function doesn't round up but only down, that's another reason we won't 'reach' 256.

Hope that helps.