Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops The Refactor Challenge Solution

Kipaya Kapiga
Kipaya Kapiga
6,066 Points

Getting 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
Agnes Demes
6,612 Points

Hi I think the problem is that your have the document write html within the curly braces not after.

Agnes Demes
Agnes Demes
6,612 Points

It 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
Kipaya Kapiga
6,066 Points

Thanks, that's an excellent analogy!

Max Gerek
Max Gerek
2,718 Points

Hi,

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
Kipaya Kapiga
6,066 Points

Thanks, 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.