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 trialHannah Mackaness
4,437 PointsI've made a solution but I don't know why it works..or how the incremental is working..
I don't understand what I have done here..this code works and I have been playing with the last line (while counter < 4) with the following results:
4 = 10 dots 5 = 15 dots 6= 21 dots 10= 55 dots
Can someone please help me understand what is happening? Thanks :)
var html = '';
var red;
var green;
var blue;
var rgbColor;
var counter = 0;
do {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);
counter+=1;
} while (counter< 4)
2 Answers
Gunhoo Yoon
5,027 PointsI think that has do with where you are rendering your dots with document.write().
var html = '';
var red;
var green;
var blue;
var rgbColor;
var counter = 0;
do {
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);
counter+=1;
} while (counter< 4)
Pay close attention to html
variable, specifically how it looks like on every iteration and document.write()
that comes right after it.
On first iteration of do....while
loop, it will have 1 div element and also 1 dot on web page since you rendered it with document.write()
On second iteration, notice how you used html += div...
. This will add another div element on top of previous html
variable since it's like saying html = html + div....
. So your html
variable now has 2 div elements and at this point you have 3 div elements rendered on your page 1 + 2.
On third iteration,
- number of div element in html variable : 3
- total number of div element rendered on page: 1 + 2 + 3 = 6 dots
On fourth
- number of div element in html variable : 4
- total number of div element rendered on page: 1 + 2 + 3 + 4 = 10 dots
And you will see the pattern now.
On fifth
- number of div element in html variable : 5
- total number of div element rendered on page: 1 + 2 + 3 + 4 + 5 = 15 dots
So this happens due to your decision to render what you built in html
on every iteration.
document.write(html);
So how would you fix this? Hint is change position of above line.
Also, try to keep the indentation rule.
Hannah Mackaness
4,437 PointsGreat, thank you for your time!
Gunhoo Yoon
5,027 PointsNo problem comment me if you cannot fix it.