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 Solution

Sean Flanagan
Sean Flanagan
33,235 Points

What's wrong with my code?

Hi.

Can anyone tell me please why this code doesn't work?

https://w.trhou.se/gytu08mz1n

Cheers

Sean

2 Answers

Steven Parker
Steven Parker
231,236 Points

The issue is on this line:

  red, green, blue = Math.floor(Math.random() * 256 );

This line probably doesn't do what you think it does. The values for "red" and "green" are evaluated, but nothing is done with or to them. Finally, "blue" is assigned to a random value. You might have been thinking this would assign all 3 at once. You can do that, but with this syntax:

  red = green = blue = Math.floor(Math.random() * 256);

But you probably don't want to do this because by giving them all the same random value, you will get a shade of gray instead of a color.

So what you most likely want here is 3 individual assignments:

  red = Math.floor(Math.random() * 256);
  green = Math.floor(Math.random() * 256);
  blue = Math.floor(Math.random() * 256);
Sean Flanagan
Sean Flanagan
33,235 Points

I see what you mean.

Thanks Steven.

Sean :-)