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, Part 2

Corbin Jensen
Corbin Jensen
12,967 Points

Refactoring Loop Part 2 not working

I can't figure out why the code below isn't working. I've copied the code but none of the circles are showing up.


var html = '';
var rgbColor;

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

function randomColor() {
  var color = 'rgb(';
  color += randomRGB + ',';
  color += randomRGB + ',';
  color += randomRGB + ')';
  return color;
}

function print(message) {
  document.write(message);
}

// use 10 because we want 10 circles
for (var i = 1; i <= 10; i += 1) {
  rgbColor = randomColor();
  html += '<div style="background-color:' + rgbColor + '"></div>';
}

print(html);

Hey Corbin,

The HTML elements that I'm certain are being used in your html variable have been stripped from your answer because code needs to be formatted properly when posting to the forums in order for it to render correctly.

The easiest way for us to see your code (if you're working on this in Workspaces), is to post a snapshot of your workspace: http://www.teamtreehouse.com/forum/workspace-snapshots

Or, you can reformat your code and wrap your code in a code block as seen in the image below and in the Markdown Cheatsheet. Be sure to leave blank spaces above and below the first and last set of backticks. Nothing should be on the lines with the backticks except for a language declaration in the first set of backticks as you see below.

code

Damien Watson
Damien Watson
27,419 Points

Hey Corbin, you have declared some variables and functions but are not actually using it. The comment down the bottom gives you a template to 'print' 10 circles.

1 Answer

The problem is that in your randomColor function, you're not calling/running the randomRGB function each time, you're just referencing the function itself. Try adding the opening and closing parentheses after the function name:

color += randomRGB() + ',';