JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops The Refactor Challenge, Part 2

Can't get my colours to appear!

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

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

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

document.write(html);

1 Answer

Brendan Whiting
MOD
Brendan Whiting
Front End Web Development Treehouse Moderator 84,061 Points

Here's an example of one of the html elements that's being added to the document from your code when I run it:

<div style='background-color:79'></div>

The problem is that "79" isn't a color. What you need is to use that number within the rgb function. The ultimate output we want is something like this:

<div style='background-color: rgb(2,148,125)'></div>

Your randomColour function is just returning a number. See if you can make it return a string that looks like rgb(2,148,125) with 3 random numbers in it.

P.S. The way I figured out what HTML your code was actually producing was using the Chrome Developer Tools. Let me know if you need more help.