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 Working with 'for' Loops The Refactor Challenge – Duplicate Code

My Solution for the refractor challenge

let html = '';

for (i = 1; i <= 10; i++) {
  const randomRGB = () => Math.floor(Math.random() * 256);
  html += `<div style="background-color: rgb( ${randomRGB()}, ${randomRGB()}, ${randomRGB()} )">${i}</div>`;
}

document.querySelector('main').innerHTML = html;

I tried to minimize the code by interpolating the randomRGB function into the div style three times.

2 Answers

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,723 Points

Nice! Maybe now try to make it a. reusable function with parameters?

Since We are on this topic. Can somebody please explain to me about reusable functions?? like where do you save your functions to reuse them? is there a program for making your own library's or somehting?