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

Greg Clinton
Greg Clinton
8,091 Points

My refactor answer

here is what i came up with. It's works, any comments?

var i = 0;
var html = '';
var red;
var green;
var blue;
var rgbColor;

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

document.write(html);
Sean T. Unwin
Sean T. Unwin
28,690 Points

I have formatted your post to display the code properly. Read how to post code to the forum if you need help in the future.

Would you mind letting us know what this is a refactor of?

It works nicely by the way. :)

Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,708 Points

Sean T. Unwin – the forum needs a sticky for this exact reason (code posting) and many others!

Greg Clinton
Greg Clinton
8,091 Points

A sticky would be nice about the code format, I have read it and will do it next time.

This is my answer for the refactor challenge on the loops javascript course. I though each course had a section but I guess not if it's in general javascript. Thanks anyways!

Sean T. Unwin
Sean T. Unwin
28,690 Points

There is also the 'Markdown Cheatsheet' popup link that is in between the text area and buttons in order to assist when you write a comment or post.

You've duplicated Math.floor(Math.random() * 256 );. Why not put it in a variable? This would increase the performance of the script.