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

Sean Flanagan
Sean Flanagan
33,235 Points

My solution

Hi guys and girls. Here's my solution:

https://w.trhou.se/9bvy57buug

I would appreciate constructive feedback please.

Sean

2 Answers

Kevin Becerra
Kevin Becerra
14,243 Points

Your answer is great and does exactly what is being asked, but if you wanted to make it more DRY this is how it could be done. You don't need to repeat Math.floor to get the random number when you can call it from a function. This also allows you to change it up a bit. If you want to the colors to printed to come out differently just changing the function would do it all.

    let html = '';
    function rgb(){
     return Math.floor(Math.random() * 256 );
    }

   for (var i = 0; i < 10; i++) {
        rgbColor = 'rgb(' + rgb() + ',' + rgb() + ',' + rgb() + ')';
        html += '<div style="background-color:' + rgbColor + '"></div>';
    }
    document.write(html);
Yusef Daramay
Yusef Daramay
561 Points

Where are the colors generated from? And also is the part "rgb()" a predefined thing that generates numbers?

this is super smart.. simply eliminated the red, blue and green.. so so cool..