Random color pick

https://www.codecademy.com/paths/web-development/tracks/build-interactive-websites/modules/dom-javascript-events/lessons/dom-events/exercises/dom-event-types

Hi in this exercise, you want a RGB number that is less or equal to 255. But Math.floor(Math.random()* 255) gives you at the highest the number 254 right?

That is correct.

To get a number between 1 and 255:

Math.floor((Math.random() * 255) + 1);

But RGB also includes “0”, so this will give a number between 0 and 255:

Math.floor(Math.random() * 256);
OR
Math.round(Math.random() * 255);

Unfortunately I don’t have access to paths, so I can’t see all of the criteria for the lesson.

1 Like