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 Introduction to Programming Objects and Arrays Quiz

I don't get how this is right?

What is the console output for the following code?

var fruits = ["Apple", "Orange","Banana","Grapes","Pomegranate"];

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

Well done! That's the correct answer.

A
Apple Orange Banana

Is not this suppose to loop from 0 to 3 which would be " Apple, Orange, Banana, Grape"

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Gary;

The correct answer is Apple Orange Banana. Since it is a zero based array, it starts at 0, apple, and continues while i is less than 3 and then it stops. The index numbers for these array items would be:

0 - Apple

1 - Orange

2 - Banana

3 - Grapes

4 - Pomegranate

If you wanted Grapes to be included in the output you would have to have it count until i < 4.

Does that help clarify anything?

Ken

This definetely helped. Thank you

Sreng Hong
Sreng Hong
15,083 Points

Hi Gary

The condition is i < 3, so 3 is not less than 3.

3 < 3 => false

Do you get what I mean?

Sreng Hong
Sreng Hong
15,083 Points

Grape supposes to be friuts[3], but i only i = 0, 1, 2

Thank you Sreng

Hi Gary,

In the for loop control i will only get to two (i < 3).

fruits[0] = "Apple"; fruits[1] = "Orange"; fruits[2] = "Banana"; fruits[3] = "Grapes"; fruits[4] = "Pomegranate";

Thank you Jeff