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

Abdullateef Almalouhi
Abdullateef Almalouhi
1,955 Points

My code is not working

I would appreciate it if somebody could tell me what is wrong with my code.

Thanks in advance

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

function colors() {
var red = Math.floor(Math.random() * 256 );
var green = Math.floor(Math.random() * 256 );
var blue = Math.floor(Math.random() * 256 );
return 'rgb(' + red + ',' + green + ',' + blue + ')';

}

for (var counter=0; counter < 10; counter +=1) {
     html += '<div style="background-color:' + rgbColor + '"></div>';
}
  document.write(html);

1 Answer

James Carney
James Carney
16,255 Points

You don't need to have a function for this. A simple for loop will do the job. I'd make the for loop say something like this:

for( var counter = 1; counter <= 10; counter += 1) {

//Your code here

}

document.write(html);
James Carney
James Carney
16,255 Points

Inside the for loops, I'd copy and paste all the code from the function you wrote and put it in there.