Listing everybody


#1

Where am I going wrong here - the error message I'm getting is "Your code threw the following error when trying to call list(): TypeError: Cannot read property 'firstName' of undefined"

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);
}

var list = function() {

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

list();


#2

Remove the semi colon here.


#3

Thanks bandit, I've removed the semi-colon but I'm still getting 'TypeError: Cannot read property 'firstName' of undefined'. This is confusing me.


#4

This here,

Should be contactsLength

instead of,


contacts.length

#5

I've updated it but still getting the same message - totally baffled by this! :slight_smile:

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);
}

var list = function() {

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

};
list ();


#6

The following semi-colon is optional, and it's actually good coding practice to keep it there (albeit optional).
You can learn more about it under the following Codecademy blog article:
https://www.codecademy.com/blog/78

var contactsLength = contacts.length;

There is a semi-colon that is causing you major pain. I commented out some of your code to see what the console.log was printing from your contacts array...and it kept coming out as "undefined". Took me a while to notice, but you have a semi-colon that shouldn't be there. So, basically, you're terminating the loop and condition before it even runs:

for (i = 0; i < contactsLength ; i++); {
    //printPerson(contacts[i]);
    console.log(contacts[i]);
}

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

Tricky little bugger!

i++); {


#7

Aah, thanks myssandria, that was driving me mad! :joy:


#8

I know the feeling. Welcome to the world of coding...one small "typo", and everything breaks loose!


#9

wouldn't you need to add var to i in the for loop?

You have

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

doesn't it need to be:

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

#10

Yes, it does, actually. Looks like we forgot one more "typo"! :sweat::sleepy::cry:

We're lucky that Javascript is so flexible it lets the code work without declaring the for loop variable i, but it is really best to have it there xD

The keyword var isn't necessarily required, but there are times you will need to use it to achieve the appropriate scope. For more information, refer to the following question by a Codecademy user:

Don't have to write "var" to declare variables?


#11

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