## 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.

# objects-and-arrays Quiz

Surely, this array should stop at item number 4?

http://gyazo.com/0d8ab966205ba2ed26486930d0b9576d

I have gone round this quiz about four times, tried every answer I think - they all say I am wrong :( ...

I'll duplicate the code here so it's easier to refer to:

```for ( var i=0; i < 3; i+=1) {
console.log(fruits[i]);
}
```

Here is a more simple way of thinking of what the logic does when you are looping:

i = 0

Loop 1

• return "Apple" ( at index 0 )
• Loop complete, now make i + 1
• i = 1

• is i <3?

• true

Loop 2

• return "Banana" ( at index 1 )
• Loop complete, now make i + 1
• i = 2
• is i <3?
• true

Loop 3

• return "Orange" ( at index 2 )
• loop complete, now make i + 1
• i = 3

• is i < 3?

• i is not less than 3

• i is equal to 3

• return false

• don't execute next loop

*If the code stated that i <= 3 , then it would loop through again and return the 4th index. So actually, the code stops at 3.

aha, so the "loop-counting" starts at 1, but the place assigned starts counting at zero? (I'm speaking in noob-lingo here, am not fully conversant with the precise terminology yet)

Well, basically it comes down to the variable 'i' - which in this case is used as your counter.

When the first loop starts, i = 0 so it runs through the loop. The loops then prints the item at the array index 0 "Apple" ( since i=0 ). Before the end of the loop, you add + 1 to the i variable, making the i = 1

Then it checks to see if i < 3. At the start of the second loop, i = 1, so it runs through the loop. The loop then prints the item at the array index 1 "Banana" ( since now i = 1 ). Before the end of the loop, you add another +1 to the i variable, making the i = 2.

Then it checks to see if i < 3. At the start of the third loop, i = 2, so it runs through the loop. The loop then prints the item at the array index 2 "Orange" ( since now i = 2 ). Before the end of the loop, you add another +1 to the i variable, making the i = 3.

Then it checks to see if i < 3. At the start of the fourth loop, i = 3, so it returns a value of 'false' and doesn't execute the loop, and since this is the end of the program, it ends.

Does that clarify it a bit more? The loop continues to execute as long as i < 3.

Thanks very much Rhys. That makes sense.

You guys are so awesome!

Thank you Rhys for being so thorough :D

Best,

Elizabeth