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

The Refactor Challenge, Part 2 with Dave McFarland

What is wrong with my code, I copied everything and I can't spot any errors.

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);
}

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

print(html);

1 Answer

For future reference, please DO format your code better, and highly avoid mutliple statements on one line.

Had you formatted your code better, you'd have fewer problems with debugging and spotting errors.

The issue is that your for loop is incomplete, it ends just before the loop condition iterator.

The easiest way to mock and debug your code is to open a new browser tab on about:blank page, enter the console, pase your code in, and then follow up on console errors. Then fix them.

I am sorry, I didn't format it that way, when I pasted it to the forum it collapsed into that form. I didn't understand why it did that. I still don't see the mistake in the for loop. I am missing it can you show me where that is? I wrote it out again, hopefully it doesn't collapse the code like it did? '''javascript <p>this is javascript</p> 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); }

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

print(html); '''

I tried to rewrite it in the comment, it didn't format it right. I see they have formatted it on the page, you might see it better today, idk? Can you tell me there is something wrong with it or write it out for me? I tried what you suggested, there was no error messages but it still didn't run the program, idk?

Your code works correctly (the one formatted above). It creates many divs with random background colors. Divs have no css and no height and width, so you cannot see them, if that's your problem.

Look here to confirm:

https://jsfiddle.net/maciejsitko/cj9bwtz8/1/

NOTE: I had to use innerHTML because jsfiddle disallows document.write (for good reason after all).

Also, look for proper function names that cannot be overriden, as you see print function is the native DOM property. Might be not a good idea to use that. Alias it as something else, there i used make because of the clash.