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 trialKipaya Kapiga
6,066 PointsGetting too many DIVs
Running into a weird problem even though I think I've done exactly the same thing as in the video. Basically, when I set the condition to (i < 10) I get 45 divs, and when I set it to (i < 20) I get 210 divs. Did anyone else run into this? Here's what I wrote:
var html = ''; var red; var green; var blue; var rgbColor; var i;
for ( var i = 1; i <= 10; i += 1 ) { 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>'; document.write(html); }
3 Answers
Agnes Demes
6,613 PointsHi I think the problem is that your have the document write html within the curly braces not after.
Agnes Demes
6,613 PointsIt is the order of the operations that making the difference. Printing the colors everytime the program goes through the sequence and then doing this 10 times due to the for loop or / opposing to going through the sequence , completing it 10 times due to the for loop and then printing it out. Say using a worldly example: if you go to the shops and every time you buy an item you pay for it and place it in a bag you will have many shopping bags to carry or if you buy a few items and place it in the bag you will have much less. The shopping bag is like the printing html in this example. I hope this metaphor helps. :) cheers
Kipaya Kapiga
6,066 PointsThanks, that's an excellent analogy!
Max Gerek
2,718 PointsHi,
I understand the analogy, but I'm not sure I understand how the browser is running the code.
With the document.write inside the brackets I see: generate random color values > assign to rgbColor > write the color > add one to the var i > re-run
With the document.write outside the brackets I see it as: generate random color values > assign rgbColor > add one to var i > generate random color value ....and repeat that when it finishes and gets to document.write it would only be writing the last color.
I understand I am seeing this wrong, how does the browser read the program with the document.write on the outside.
Kipaya Kapiga
6,066 PointsThanks, that was it! Any idea why that's the case? It seems more intuitive that you would want document.write inside the loop since it's being performed multiple times. Outside the loop, it's only called once.