Listing Everybody Issue


#1


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


I keep on obtaining: "Oops, try again. Your code threw the following error when trying to call list(): TypeError: Cannot read property 'firstName' of undefined"
Could someone please tell me what's wrong with 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);
}

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


#2

The maximum number should be less than (<) contactsLength, not <=. The for-loop goes beyond the array if you kept that.

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

#3

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