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

Joshua Mclendon
Joshua Mclendon
46,708 Points

I don't understand why this is wrong. Everything looks fine, but for some reason it's not printing the divs with colors.

//My code for refactor challenge part 2

var rgbColor;
var html = "";
function randomRGB(){
 return Math.floor(Math.random() * 256); 
}
function randomColor(){
 var color = "rbg(" + randomRGB() + ", " + randomRGB() + ", " + randomRGB() + ")";
 return color;
}

function print(message){
 document.write(message); 
}

for(var i = 0; i < 10; i++){
rgbColor = randomColor();
html += '<div style="background-color:' + rgbColor + '"></div>';
}
print(html);

3 Answers

Your random color has a typo.

var color = "rbg(" + randomRGB() + ", " + randomRGB() + ", " + randomRGB() + ")"; //should be "rgb" not "rbg"

//try this

var color = "rgb(" + randomRGB() + ", " + randomRGB() + ", " + randomRGB() + ")";

I hope this helps.

Zachary Billingsley
Zachary Billingsley
6,187 Points

Hello!

I see just two things.

First, when setting your color variable in the "randomColor" function change the "rbg" to "rgb".

And second, when you create your divs you may want to give them some height, unless there is style sheet that specifically gives divs a height so you can see them.

html += '<div style="background-color:' + rgbColor + ' ; height:25px"></div>';

Hope that helps!

Joshua Mclendon
Joshua Mclendon
46,708 Points

Ohh! Thanks. Silly syntax error haha. Sometimes it's good to have extra eyes