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

Edward Santos
Edward Santos
1,415 Points

Why does the page load 10 different dots, and not just one dot with the later iteration of the color?

I am not understanding why each dot appears. I though only the last iteration of html would display

Steven Parker
Steven Parker
221,070 Points

Please show your code so we can see what's going on.

Remember to use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

1 Answer

Jerry Wu
Jerry Wu
1,845 Points

If I understand what you are asking, your assumption is that only one dot should be created with the last random color generated. For that to happen, document.write(html); would have to write the following line exactly one time:

'<div style="background-color:' + rgbColor + '"></div>'

However, when you run the for loop, the code has a += to write the above line to the html variable the number of times you are running the for loop for. Per the instructions, it is ran 10 times. This will make it such that:

var html = '<div style="background-color:' + rgbColor + '"></div>''<div style="background-color:' + rgbColor + '"></div>''<div style="background-color:' + rgbColor + '"></div>''<div style="background-color:' + rgbColor + '"></div>''<div style="background-color:' + rgbColor + '"></div>''<div style="background-color:' + rgbColor + '"></div>''<div style="background-color:' + rgbColor + '"></div>''<div style="background-color:' + rgbColor + '"></div>''<div style="background-color:' + rgbColor + '"></div>''<div style="background-color:' + rgbColor + '"></div>'

Each rgbColor stores a different randomized color based on the randomized red, blue, and green variables. Therefore, you end up with 10 iterations of 10 randomly generated colors.