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

Saloni Mude
Saloni Mude
9,933 Points

Don't know why it isn't printing anything to the screen. Please help.

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

The console won't point to any errors either.

Hi, I know it's been 5 days and you've probably figured out the answer already, but just in case, i tried out your code and the only problem I could find was you included an unnecessary space in:

var color = 'rgb (';

it should be:

var color = 'rgb(';

Once I fixed this in your code everything ran fine.

4 Answers

is html a string in your code

Yep! It's a string because there's quote marks (' or ") around them.

"print" Isn't a function that will print things to the screen in JS.

The "print" function will actually print output onto a piece of paper LOL

Try using the "document.write" function instead.

Good luck! ~Alex

Saloni Mude
Saloni Mude
9,933 Points

But I created a new function called print earlier in the program.

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

Never mind. I didn't know you pre-defined the function :P

i guess u can try using document.writeln() but ultimately you used document.write(message); so do the same in this case

That's wrong. I just found out the JS searches through your own functions first, then if you didn't define the function, it will check the built-in functions. Also, document.writeln() isn't a function in JavaScript, but document.write() is a function.

Then the

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

part is wrong.

Try this instead:

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

Good luck! ~Alex