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 Solution

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

why is it not working??? wot m i doing wrong

var html = ''
var randomColor;
var rgbColor;

function getColor(){
 randomColor = Math.floor(Math.random() * 256 );
 rgbColor = "rgb(" + randomColor + "," + randomColor + "," + randomColor + ")";
  return rgbColor;
}
for (var i =0; i<100; i+=1){

html += '<div style="background-color:' + rgbColor + '"></div>'
}
document.write(html);

5 Answers

Steven Parker
Steven Parker
231,269 Points

There are a few issues here:

  • the variable rgbColor is defined but never assigned
  • the variable "randomColor" is defined once in the function but used 3 times
  • the function getColor is defined but never used
  • the loop builds a string using rgbColor which was never assigned
  • if dimensions are not being set by CSS, empty div elements will have no height and not be visible
Saqib Ishfaq
Saqib Ishfaq
13,912 Points

is this wot u mean by assigning? i used "randomColor" 3 times to make it rgb color....is this not how we get 3 values?

var html = ''
var randomColor;
var rgbColor;

function getColor(){
 var randomColor = Math.floor(Math.random() * 256 );
 var rgbColor = "rgb(" + randomColor + "," + randomColor + "," + randomColor + ")";
  return rgbColor;
}
for (var i =0; i<100; i+=1){

html += '<div style="background-color:' + rgbColor + '"></div>'
}
document.write(html);
Steven Parker
Steven Parker
231,269 Points

The "assignment" I'm referring to is where you use the math formula to give randomColor a value. But then when you use that value 3 times each one will be the same. This will give you a color that will always be a shade of grey, is that what you want?

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

nops, m trying to complete the challenge and come up with different color each time i load or atleast 3 different colors. and get rid of extra code mentioned in the challenge:/

Steven Parker
Steven Parker
231,269 Points

I suspected you intended them to be different, and that's why I listed re-using the same value 3 times as one of the issues.

You say this is for a challenge? But the button in the upper right links to a video.

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

Yeh the video was to reduce the code using function n loops. Completed the 1st task but on 2nd task it got bit complicated.I just saw the video n saw the issue I was having. Would hv liked to sort it on my own tho:/

Steven Parker
Steven Parker
231,269 Points

I didn't give you any answers, just hints about the issues. You'll still be resolving it yourself.

Hunter Shaw
Hunter Shaw
2,187 Points

I solved your problem; however, I will not give you my code. Giving answers away doesn't help you learn. However, I can say that you're missing an argument. How are you suppose to call the function and return a value if the return is rbgColor. Your function doesn't know to return it. The whole point of the function you're using is to return the value.