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

Alex Vien
Alex Vien
7,615 Points

Here's my code for this challenge. Is this DRY?

var html = ''; var red; var green; var blue; var rgbColor;

function getRgbNumber() { return Math.floor(Math.random() * 256); }

function getRgbColor() { return rgb(${getRgbNumber()},${getRgbNumber()},${getRgbNumber()}); }

for ( i = 0; i < 10; i += 1 ) { html += <div style="background-color:${getRgbColor()}"></div>; }

document.write(html);

1 Answer

Steven Parker
Steven Parker
231,153 Points

It's hard not to be "DRY" with a project this small, but perhaps you had doubts about the three calls to "getRgbNumber". That doesn't violate "DRY" because you need three separate random numbers in that function.

But you could compact the code a bit by removing the declarations for the variables that aren't used in the code (all but "html").

Alex Vien
Alex Vien
7,615 Points

Awesome! Thanks Steven. Totally agree. When I reviewed the solution I noticed I totally missed that and could've deleted them haha. Thanks!

Steven Parker
Steven Parker
231,153 Points

Alex Vien — Glad to help. You can mark the question solved by choosing a "best answer".
And happy coding!