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 trialFarid Lavizadeh
12,006 PointsHow is the last array item shown when value "4" was changed to friends.length?
When he has 4 friends, the value of condition in the loop is 4. He adds a new friend and changes the condition to " i < friends.length". The value of friends.length should still be 4. No? If so how is the last name "jon" is displayed?
My understanding is that for it to work it should say "i =< friends.length". What am I missing here?
3 Answers
Hugo Paz
15,622 PointsHi Farid,
Its due to the fact that an array always starts at 0.
friend.length returns 5 because there are 5 names in the array, so in reality you have i<5.
So when the for loop runs through the array it goes like this,
friends[0], friends[1], friends[2], friends[3], friends[4] which is 'Jon'.
Farid Lavizadeh
12,006 PointsOh so unlike Array value, Length value starts with one. Thank you so much. Somehow I assumed the Length also starts with 0. Duh me.
Prabhakar acharya
Courses Plus Student 2,801 Pointsobject.length is a good way to display array, we can keep adding object to the array and .length method will give you exactly how many object are stored. he used i<4 because we know we have 4 objects in array, if the size of the array increases then i<4 will still display 4 objects, so in this case in order to display all the objects .length is best method.