## 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.

# Doing it with a while loop

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

i = 0;
i += 1;
while (i <= 10) {
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);
```

not working

Code

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

```      ```html
<p>This is code!</p>
```
```

Hi Nick, when we are working with loops, especially `while` loops, it's important that we increment the counter inside of the body of our loop, otherwise we will end up with an "infinite loop." In this case the statement `i += 1;` is where we are incrementing the counter variable, so in order for it to keep us from having an infinite loop, we need to place that statement somewhere between the curly braces of our while statement, perhaps right above the `red =` initialization.

Notice how in the code you have now, you are checking that `i` is less than or equal to 10, but if we follow the logic of the code, we first have `i` = 0, then we increment it to 1, but then when we are inside the while loop we are not changing it again. Every time we get to a new iteration of the while loop, `i` still has a value of 1, and so the loop never finishes!

Hope this helps, and thanks for being a Treehouse member!

What do you want them to i = 0 and i+= 1? This isn't good and nice solution. You can use for() loop insted of while() loop. Like this, for(var i = 0; i < 10; i++). Hope this help!

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

i = 0;
i += 1;

while (i <= 10) {
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);
```
```var html = '';
var red;
var green;
var blue;
var rgbColor;

i = 0;
while (i <= 10) {
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>';
i ++;
}

document.write(html);
```

i++ is same like i += 1