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, Part 2

The Refactor Challenge clarification question!

for (var i = 0; i < 10; i += 1) {
  red = randomRGB;
  green = randomRGB;
  blue = randomRGB;
  rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
  html += '<div style="background-color:' + rgbColor + '"></div>';
}

In this loop, how does JavaScript generate rgb color ?

I do understand that it uses a "function random RGB" to return random color since rgb has its highest 255 value. But how does it understand that I need green, red or blue color.

fixed code formatting

1 Answer

Kent ร…svang
Kent ร…svang
18,823 Points

randomRGB returns a number between 0 and 255. Both red, blue and green has the exact same value span (0 - 255), so it do not, and does not need to understand what color you are assigning it to.

Hope this helps