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

Ahmed Hassan
Ahmed Hassan
5,533 Points

challenge done! Refracted

var html = '';
var red;
var green;
var blue;
var rgbColor;

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

document.write(html);

1 Answer

Hi Ahmed,

You have just a piece of code missing in order for the divs to show. You either have to give each div a defined height or put some content in between the beginning div tag and the ending div tag. I chose to go with adding at least a height to each div because I added just a couple extra styles to make each div not only appear but appear as circles!

var html = '';
var red;
var green;
var blue;
var rgbColor;

for(var i=0; i<10; i+=1) {
 red = Math.floor(Math.random() * 256 );
 green = Math.floor(Math.random() * 256 );
 blue = Math.floor(Math.random() * 256 );
 rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
//added height (required) and width to style so that it will appear
//setting border-radius to 50% makes an element appear as a circle :)
 html += '<div style="height: 50px; width: 50px; border-radius: 50%;background-color:' + rgbColor + '"></div>';
}

document.write(html);

Here is a version I did of this challenge where each circle changes color as you hover over it: http://marcusparsons.com/projects/randomcolorcircles/index.html If you look at the source of the page, I put comments on most of the code there.