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 trialEdward Santos
1,415 PointsWhy 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
1 Answer
Jerry Wu
1,845 PointsIf 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.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsPlease 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. Or watch this video on code formatting.