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 trialSaloni Mude
9,933 PointsDon'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.
4 Answers
saif ahmad
6,352 Pointsis html a string in your code
Alexander Davison
65,469 PointsYep! It's a string because there's quote marks (' or ") around them.
Alexander Davison
65,469 Points"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
9,933 PointsBut I created a new function called print earlier in the program.
function print(message) {
document.write(message);
}
Alexander Davison
65,469 PointsNever mind. I didn't know you pre-defined the function :P
saif ahmad
6,352 Pointsi guess u can try using document.writeln() but ultimately you used document.write(message); so do the same in this case
Alexander Davison
65,469 PointsThat'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.
Alexander Davison
65,469 PointsThen the
html += '<div style="background-color:' + rgbColor + '"></div>';
part is wrong.
Try this instead:
html += '<div style="background-color:"' + rgbColor + '></div>';
Good luck! ~Alex
Jonathan Shull
Courses Plus Student 12,739 PointsJonathan Shull
Courses Plus Student 12,739 PointsHi, 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.