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 Solution

solution

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

function generateRGB(color){ color = Math.floor(Math.random() * 256 ); return color; }

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

document.write(html);

Alexandra Wakefield
Alexandra Wakefield
1,866 Points

It seems I did the same. The only difference is I omitted the variables red, blue, green and just when with repeating the function name.

var html = '';

function color() {
  return Math.floor(Math.random() * 256) + 1;
}

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

document.write(html);

5 Answers

var html = '';
for(var i = 0 ;  i < 100 ;  i++){
    function rand(){
        return Math.floor(Math.random() * 256);
    }
    html += '<div style="background-color:rgb(' + rand() + ',' + rand() + ',' + rand() + ')"></div>';
}
document.write(html);

:thumbsup: Looks great!

thx Alexander!

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

for (var i = 1; 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);

This was my code, but i am getting an "allocation size overflow". The code is the same as the instructors.

Edwin Carbajal
Edwin Carbajal
4,133 Points

Hi Ryan,

Not sure if you have found a solution to your problem but let me show you anyway. It looks like you have a typo in your for loop. Here:

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

The i variable should increase each loop. And the correct syntax for this is:

i += 1

better to write for (var i = 1; i <= 10; i++)

i++ // that is more common practice 

is the same as

i += 1

w0w, i wish my brain is like yours Alexandra and Vaetev :) I solve mine like this so poor logic >.<

"var html = ''; var red; var green; var blue; var rgbColor; var count=0;

while (count<100){

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>'; count+=1; }"