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
krishnapriya sivasubramanian
7,739 PointsRefactoring challenge #2 is not working
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 <= 10; i += 1) { rgbColor = randomColor(); html += '<div style="background-color:' + rgbColor + '"></div>'; } print(html);
1 Answer
Joe Beltramo
22,823 PointsYou are close! I am not sure exactly what the challenge is asking of you but there are 2 things happening here that are causing you to perceive your code as not working.
The first thing is that the output for the colors created have a space between the rgb keyword and the beginning parenthesis (. So, firstly remove the space between rgb and (:
// This:
var color = `rgb ( ...
// Should be this:
var color = `rgb( ...
The second thing, is that the default display property for a div is inline meaning that it will expand to the width and height of it's internal content. So in order to see the background color correctly, you will need to add content or explicitly give each div a width and height like so:
... '<div style="background-color:' + rgbColor + '; width: 100px; height 100px; "> ... '
Changing those things should result in something you are looking for, if the challenge is to see something. Otherwise, just do the first change I mentioned and that may resolve your issue.
Hope that helps! Feel free to ask if I should further clarify anything said here.