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 trialDerrick Johnson
4,347 PointsEasy to read and functional solution
Any way to not have to call randomNum()
3 times?
function randomNum() {
return Math.floor(Math.random() * 256 );
}
function createColor() {
[r,g,b] = [randomNum(),randomNum(),randomNum()];
document.write(`<div style="background-color:rgba(${r},${g},${b},1)"></div>`);
}
for (i=0;i<10;i++) {
createColor();
}
1 Answer
Steven Parker
231,269 PointsThe main value in defining randomNum at all is to use it multiple times.
Anything I can think of to avoid the multiple calls (like a loop) would not be as concise as the code you have already.
But you might want to add let to the line where you assign r, g, and b. to also define them