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

Mia Bucci
Mia Bucci
4,300 Points

I tried to limit to creating one random colour but got stuck โ€“ย  can someone help review my code please?

var html = ''; var rgbColor;

function getColor () { rgbColor = Math.floor(Math.random() * 256 ); html = '<div style="background-color:' + rgbColor + '"></div>'; return(html); }

for (var i=0; i < 10; i++ ){ document.write(getColor() ); }

4 Answers

Steven Parker
Steven Parker
229,981 Points

An RGB color has 3 components, red, blue, and green. So you need 3 numbers to create one color.

You also need to separate the numbers with commas, and enclose the set of them in "rgb()" to make up the value part of the property setting.

Hi Mia Bucci,

I have had a look at your code. There is a problem with how you generate the rgbColor variable.

In order to create an RGB color, you need three values between 0 and 256. This means, that your variable rgbColor will not be correctly generated with your code.

In your code, the value of the rgbColor variable will be one value, i.e. one random number:

rgbColor = Math.floor(Math.random() * 256 )

Instead, it should be:

rgbColor = "rgb(";
  rgbColor += Math.floor(Math.random() * 256 ) + ",";
  rgbColor += Math.floor(Math.random() * 256 ) + ",";
  rgbColor += Math.floor(Math.random() * 256 ) + ")";

Doing this will result in a string, that you can use to insert into the page's html.

If you like have a look at my code (below). I have separated the generating of random number into a separate function RandColor() to make the code easier to read. If you want to generate ten different colors on the page, you wrap the function call into a for loop. If you only want one color to display, just leave the for loop out, as the function will only run once then.

var html = '';
var rgbColor;

function RandColor() {
  return Math.floor(Math.random() * 256 );
}

function getColor() {
  var rgbColor = "rgb(";
  rgbColor += RandColor() + ",";
  rgbColor += RandColor() + ",";
  rgbColor += RandColor() + ")";

  html = '<div style="background-color:' + rgbColor + '"></div>';
  return html;
}

document.write(getColor());
Mia Bucci
Mia Bucci
4,300 Points

Thanks! I didn't understand the color components but I watched the next video and now i'm clearer!

Steph Sakai
Steph Sakai
1,213 Points

var html = ''; var color; var rgbColor;

'"></div>';

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