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

garryl torres
garryl torres
1,090 Points

how does the program know that variable 'i' is related to each div color?

I am a bit confused about this challenge. To re re factor the code, the instructor added a 'for' loop with the condition of 'i' as a variable right? But, how does the program know that var 'i' is related to each div and color if it wasnt declared at the top with all the other variables?

cornelis Etta
seal-mask
.a{fill-rule:evenodd;}techdegree
cornelis Etta
Front End Web Development Techdegree Student 7,093 Points

They are not really related in that way. see the i variable as a conditional variable which you declare using var i = 1; syntax. All you are telling your loop is print/create a new div color as far as the i variable is less than or equal to 10. The moment that condition is false the loop stops.

2 Answers

It knows because u loop through the array. if u loop through array of list item elements, each iteration the "i" represent different list item. if ur in i[0] then the "i" represent the first list item.

Jamie Reardon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Reardon
Treehouse Project Reviewer

The variable i declared in a loop is represented as an index value. In arrays, you have index positions for elements of the array. Arrays are zero indexed meaning they start with 0 (first value is 0). On each iteration of the loop, the loop adds 1 to the i variable. For example, after 2 iterations the index variable holds the value 2, thus selecting the 3rd element in the array.