Building an address book | 4. listing everyone | type error


#1

Sorry if this is an overlooked typo!

The code seems to work fine but I would like to "officially" move to the next level.
Here is my error message:
Oops, try again. Your code threw the following error when trying to call list(): TypeError: Cannot read property 'firstName' of undefined

My Code:

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

printPerson(contacts[1]);

function list(){
var contactsLength = contacts.length;
for(var i=0;i<= contactsLength; i++ ){
printPerson(contacts[i]);

}
list();

}

Much gratitidue


#2

HI thsi part

function list(){
    var contactsLength = contacts.length;
    for(var i=0;i<= contactsLength; i++ ){
        printPerson(contacts[i]);
    }
}
list();

you should put the call function list(); outside the function.... then the for loop

 for(var i=0;i<= contactsLength; i++ )

in the array contacts you got two name and the first is at index 0 and the second at index 1.. so if you put i <= contactsLength it will check for 0, 1, 2 but you dont have three name so is for that you get the error ...so remove the = to <=


#3

Thank you wizmarco for finding those 2 errors.

My main problem was getting too creative and codecademy punishes me.