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 trialPaul Costanza
6,357 PointsIs it ok to use a while loop?
For this challenge, my first instinct was to use a while loop. I got the same answer, but was wondering if that is considered wrong? or is it wrong from a professional point?
var html = ''; var red; var green; var blue; var rgbColor;
var count = 0;
while (count < 10) { red = Math.floor(Math.random() * 256 ); green = Math.floor(Math.random() * 256 ); blue = Math.floor(Math.random() * 256 ); rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')'; html += '<div style="background-color:' + rgbColor + '"></div>'; count += 1; }
document.write(html);
1 Answer
Steven Parker
231,275 PointsThat's certainly legitimate code. The advantage a for loop gives is that the initialization, conditional, and increment are all together in one place. But there's no functional difference when all are coded properly for a while
loop (as they are here).
For future reference, when posting code, always use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.
Paul Costanza
6,357 PointsPaul Costanza
6,357 PointsCool, thanks. And will do!