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

David Mendiola
seal-mask
.a{fill-rule:evenodd;}techdegree
David Mendiola
Full Stack JavaScript Techdegree Student 16,579 Points

Is this a passable solution?

var numCircles = 10;

function buildRGB(){
    return Math.floor(Math.random() * 256);
}
function getRGB() {
    return "rgb(" + buildRGB() + "," + buildRGB() + "," + buildRGB() + ");";
}
for (var x = 0; x < numCircles; x++){
    var html = "<div style='background-color:" + getRGB() + "'></div>";
    document.write(html);
}

Would this be a viable solution?

David Bath
David Bath
25,940 Points

I don't see why not! Looks good!

ha, that's awesome!

2 Answers

Did you try it? Did it return the same output as Dave's did?

If the output is the same as Dave's output, you should be good to go :)

Good luck! ~alex

Anas AlHariri
Anas AlHariri
8,776 Points

I think that he may be using an external code editor, and he could have a problem with the styling sheet. He shouldn't get the expected result unless he has the same styling effect of the project.

Good Luck

I meant by the same functionality. :)

~alex

Mine was very close, but I got a chance to make a few improvements. Thank you, David!

var html = '';

function tom() {
  return Math.floor(Math.random() * 256 );
//  var mot = Math.floor(Math.random() * 256 );
//  return mot;
}

function getColor() {
  return `rgb( ${tom()}, ${tom()}, ${tom()})`; 
}

for (var i = 0; i < 3; i++ ) {
  html += `<div style="background-color: ${ getColor() } "></div>`;
}

document.write(html);