Listing everybody instructions problem


#1

https://www.codecademy.com/courses/building-an-address-book/0/4?curriculum_id=506324b3a7dffd00020bf661

why does it tell you in the instructions "All of the items in an array are numbered, starting at 0. To cycle through all of the elements of the array, create a for loop that cycles from 0 up to one less than the number of items in the contacts array."

if you do this then only one of the contacts will show (there are two now) and you get an error after you submit


#2

To cover all corner cases for no matter how many contacts; while ensuring the for loop cycles from 0 up to one less than the number of items in the contacts array, I advise you add to the for loop expression,

i < contacts.length;

#3

Yihaa, my first comment ever.

for (var i = 0; i < contactsLength; i++) {
    //  run this code within loop body
    };

The for loop works like this, in this case it starts with the variable i which is 0, it then runs the code inside the loop body. After that it checks the variable, in this case, is it less than contactsLength? Yes is is, (because it's 0 and contactsLength is 1). Okay, increment by 1 and start over. Now variable i is 1, the code inside the loop body runs, then it checks again, is variable i less than contactsLengh? No, not any longer (because i is now 1), and the for loop stops.

Hope this helps, and please correct me if I'm wrong :slight_smile:


#4

Spot on! However, it's contacts.length and not contactsLength.

Keep on! :slight_smile:


#5

What is the final code????


#6

Oh yes of course. I wasn't very clear that I defined the variable contactsLength earlier :slight_smile:

var contactsLength = contacts.length;

Cheers!


#7

Where are you stuck? :slight_smile:


#8

I dont know where i'm stuck


#9

@jas364, please post your code and discribe the problem you have.


#10

You're welcome.

Have fun coding! :slight_smile:


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.