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

My Solution For This Challenge!!

Very efficient. Could perhaps go ever further and re-factor 'rgbColor into the document.write method like:

document.write('<div style="background-color: rgb(' + randomColor() + ',' + randomColor() + ',' + randomColor() + ')"></div>');

Unsightly maybe but uses one line of code (and one variable) less.

1 Answer

Luca Botticelli
PLUS
Luca Botticelli
Courses Plus Student 8,753 Points

I also had a different solution, putting everything inside the for Loop just like this.

for ( var outputs; outputs < 10; outputs += 1) { red, green, blue = Math.floor(Math.random() * 256 ); rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')'; html += '<div style="background-color:' + rgbColor + '"></div>'; } document.write(html)

and it seems to work fine the same