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

Luqman Shah
Luqman Shah
3,016 Points

What am I doing wrong? Nothing shows up in the preview.

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

for ( 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);

Is this not how you do it? Nothing shows up in the preview, I ad 0 to the counter, then check to see whether counter is less than 10, each time it's not, I add 1 to the counter until the loop repeats 10 times, so the all 10 colors randomly change 10 times?

2 Answers

Shane Oliver
Shane Oliver
19,977 Points

You have 2 syntaxs error in your loop declaration. Store i as a variable and remove the trailing semi-colon from i+=0;

for ( var i=0; i<10; i+=1 ) {

You won't see any output because your divs have no content and will collapse unless they are given content or a width and height.

Luqman Shah
Luqman Shah
3,016 Points

Thank you so much!! I gave up entirly on this method and started going into functions and what not when I already had the solution; just a syntax error lol.

"i" is not declared in the for loop it should be for ( var i=0; i<10; i+=1; ) { // Rest of the code here }

Luqman Shah
Luqman Shah
3,016 Points

Thank you so much!! I gave up entirly on this method and started going into functions and what not when I already had the solution; just a syntax error lol.