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 trialThu Phan
3,392 PointsMy solution for The Refactor Challenge – Duplicate Code
Here's my solution using map()
with the colors
array. I'd love to hear your feedback on the code:
let html = '';
let randomRGB;
let colors = [0, 0, 0];
for ( let i = 1; i <= 10; i++) {
colors1 = colors.map(x => Math.floor(Math.random() * 256));
randomRGB = `rgb( ${colors1} )`;
html += `<div style="background-color: ${randomRGB}">${i}</div>`;
}
document.querySelector('main').innerHTML = html;
1 Answer
Sara Masek
Front End Web Development Techdegree Student 11,513 PointsHi Thu, I really like your solution! It's very streamlined and easy to understand. I like that you defined the randomRGB and html as empty variables at the top, I feel like that's more in step with what we've learned in our Javascript Basics course. I also like that you set the colors variable as an array- I had to look up what the "map" method was, I've never used it before but it makes sense in this context.
Good work!