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

I'm trying to make this code work, but with no success, can someone help me understand what is wrong with it?

var html = ''; var red; var green; var blue; var newColor; var rgbColor= getColor();

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

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

document.write(html);

change last line to document.write(rgbColor); also I cleaned up your code, check it out here https://codepen.io/RayThomas1GTU/pen/rNNrVVW?editors=0010

I'll delete this pen later so if you need the code go ahead and copy it :)

I got carried away because I thought this was cool and made it so that if you click anywhere, the background changes to a newly generated code. You did all the hard work, nice job!

cornelis Etta
seal-mask
.a{fill-rule:evenodd;}techdegree
cornelis Etta
Front End Web Development Techdegree Student 7,093 Points

I think you are trying to do this however I tried with your code which is the second code. I change the rgbColor to getColor() which is not setting your rgbColor to just one color with the syntax var rgbcolor = getColor();

var html = ''; var red; var green; var blue; var rgbColor; function getcolor(){ 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>';

} for(i=1; i<=10; i++){ //calling my function getcolor();

} //prints out the color document.write(html);

var html = ''; var red; var green; var blue; var newColor; var rgbColor= getColor();

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

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

document.write(html);

3 Answers

cornelis Etta
seal-mask
.a{fill-rule:evenodd;}techdegree
cornelis Etta
Front End Web Development Techdegree Student 7,093 Points

I think you are trying to do this however I tried with your code which is the second code. I change the rgbColor to getColor() which is not setting your rgbColor to just one color with the syntax var rgbcolor = getColor();

var html = ''; var red; var green; var blue; var rgbColor; function getcolor(){ 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>';

} for(i=1; i<=10; i++){ //calling my function getcolor();

} //prints out the color document.write(html);

var html = ''; var red; var green; var blue; var newColor; var rgbColor= getColor();

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

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

document.write(html);

change last line to document.write(rgbColor); also I cleaned up your code, check it out here https://codepen.io/RayThomas1GTU/pen/rNNrVVW?editors=0010

I'll delete this pen later so if you need the code go ahead and copy it :)

I got carried away because I thought this was cool and made it so that if you click anywhere, the background changes to a newly generated code. You did all the hard work, nice job!

To Cornelis Etta - for some strange reason I can't score your answer. COuld you possible re-edit your comment, so I can upvote it?