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

Jess Taylor
Jess Taylor
5,160 Points

My Solution

I went for a few little functions to make it easier for modification in the future maybe?? Thoughts??

var html = '';
var rgbColor = '';

function getRandom() {
    return Math.floor(Math.random() * 256);
}

function getRGB() {
    rgbColor = "";
    for (var i = 0; i <= 2; i++) {
        if (i == 2) {
            rgbColor += getRandom();
        } else {
            rgbColor += getRandom() + ',';
        }
    };
    return 'rgb(' + rgbColor + ')';
}

function createCircles() {
    for (var i = 0; i <= 9; i++) {
        html += '<div style="background-color:' + getRGB() + '"></div>';
    };

    return html;
}

document.write(createCircles());

1 Answer

Hi Jessica,

I think little functions like that are just fine, they help keep us focused on one spot at a time, and give us tools we can use many times over.

In your getRGB function, how does this look, do you like this better? <br> function getRGB() {<br> var comma = ', ';<br> var returnRGB = 'rgb(';<br> returnRGB += getRandom(); // pick the red shade<br> returnRGB += comma;<br> returnRGB += getRandom(); // pick the green shade<br> returnRGB += comma;<br> returnRGB += getRandom(); // pick the blue shade<br> returnRGB += ')';<br> return returnRGB;<br> }<br>