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

Nour El-din El-helw
Nour El-din El-helw
8,241 Points

Why is that happening

Why do i get 64 colors instead of 10? Here is the workspace https://w.trhou.se/demhf6rxt8

2 Answers

Steven Parker
Steven Parker
229,644 Points

Are you sure you see 64? I'd expect you would see 55, because in your colorGenerator function, you keep adding to the "html" variable with the ("+=") operator. So each time you call it, instead of printing just one new color item, it prints all the ones done before again plus another one.

Use an ordinary assignment ("=") to get just 10.

Nour El-din El-helw
Nour El-din El-helw
8,241 Points

Oh! Sorry, you are right they are 55. So basically every time the loop runs a new value is assigned to the html variable with the old color(s) + a new one and it gets printed next to the already printed color(s) is that right? if so then can't I just print the color without assigning it to a variable, or is assigning it to a variable makes the code more readeable or is there a specific use for that?

Steven Parker
Steven Parker
229,644 Points

You can print it directly without using a variable, or just use an ordinary assignment ("=") instead of the special one that adds ("+="). Either way you will get just 10.

Nour El-din El-helw
Nour El-din El-helw
8,241 Points

is a way better than another somehow or is it same?

Steven Parker
Steven Parker
229,644 Points

If you don't need a value more than once, it saves a step to not create a variable for it.