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 trial

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
231,275 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.