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

My refactoring solution. Keeping it simple.

var html = '';

function colorCombination() {
 var red = Math.floor(Math.random() * 256 );
 var green = Math.floor(Math.random() * 256 );
 var blue = Math.floor(Math.random() * 256 );
 var rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
  return rgbColor;
}

for ( var i = 1; i <= 10; i += 1) {
  html += '<div style="background-color:' + colorCombination() + '"></div>';
}
document.write(html);

3 Answers

Good job! I tried looking for furthermore things you could simplify, but it turns out your code is very clean and well-organized. Keep up the great work! :)

Good luck!

Hope this helps, Alex

Gary Haag
PLUS
Gary Haag
Courses Plus Student 1,314 Points

here is mine

var html = '';

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

document.write(html);

He is asking how to refactor his coed he already has, not asking for another answer. You did reduce the code a little bit, however is isn't easy to see what's going on quickly while Alistair Holmes's code is a little easier to understand because of the function. :)

~Alex

Thanks for the input guys.. And thanks Alexander Davison for the confidence boost. I'm fairly new to programming, so it's nice to get encouragement from a fellow developer that has a lot of experience. Will keep on working hard to become a front-end dev.

Happy coding :) ~Alistair