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

Enemuo Felix
Enemuo Felix
1,895 Points

What am I doing wrong?

My code only display one color circle

var html = '';
var red;
var green;
var blue;
var count = 0;

function randomColor() {
return Math.floor(Math.random() * 256 );
}

while(count < 10) {
  red = randomColor();
  green = randomColor();
  blue = randomColor();
  count+= 1;
}
rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
html += '<div style="background-color:' + rgbColor + '"></div>';
document.write(html);

1 Answer

Hi there,

If I'm correctly guessing at the expected output, I think you just need to move these two lines inside the loop:

rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
html += '<div style="background-color:' + rgbColor + '"></div>';

The way it is right now, the loop generates random values, but nothing is done with them until they are assigned to rgbColor and added to the html variable, but since that's outside the loop it only happens once, resulting in only one div. If you move those lines into the loop, it will add a div to the html variable for each set of random colors, which I'm guessing is what you're looking for.

Let me know if that works for you!

Enemuo Felix
Enemuo Felix
1,895 Points

Thanks Katie. It worked

Awesome!