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

Jan Mc Kell
Jan Mc Kell
1,090 Points

My solution for the Refactor challenge!

So, I decided to use an array to store the red, green, and blue values for the RGB color. Each value in the array is generated by calling a function (RGBcolor()) that produces a random number between 0 and 255. The array elements are then joined into a single string, separated by commas, using the join(', ') method. This creates a valid rgb() string, which is applied as the background-color for each <div>.

I added a for loop which runs 10 times, and on each iteration, the loop number (num) is included in the HTML output to display the result of the current iteration inside the <div>.

let html = '';
let randomRGB;
let num;
function RGBcolor() {
 return Math.floor(Math.random() * 256);
};

for ( num = 1; num <= 10; num++ ) {
  let RGB = [RGBcolor(),RGBcolor(),RGBcolor()];
  let randomRGB = `rgb( ${RGB.join(', ')} )`;
  html += `<div style="background-color: ${randomRGB}">${num}</div>`;
};

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