Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,660 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
25,767 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,660 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.