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

Brok Sorensen
Brok Sorensen
1,578 Points

What am i doing wrong? This is supposed to output 10 colored circles to the web page but nothing shows up. Whats wrong?

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

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

while (counter<=10){ red = randColor; green = randColor; blue = randColor; rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')'; html += '<div style="background-color:' + rgbColor + '"></div>'; counter +=1; }

document.write(html);

when you're assigning variables such as 'red=randColor;' you're not calling it as a function, so it's returning the expression rather than the result.

Also, you should set some styles to make the divs visible when they are added to the document, in this case they have neither height nor width

1 Answer

Steven Parker
Steven Parker
231,275 Points

As Robert said, you need to call the function instead of just naming it:

  red = randColor();
  green = randColor();
  blue = randColor();

And to see the output as circles, you also need some CSS code. None is shown here, do you have it?

If not, this won't make circles but at least you can see the colors as stripes:

  html += '<div style="background-color:' + rgbColor + ';min-height:1em"></div>';