4/6 Cannot read property 'firstName' of undefined


#1

Please help. It gives me a TypeError: Cannot read property 'firstName' of undefined. What's wrong?

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() {
for(i=0; i printPerson(contacts[i]);
};
};
list();


#2

Your for loop looks like a work in progress:

for(i=0; i printPerson(contacts[i]);
};

If you do not remember how to write a for loop you should get back to the section about loops.


#4

Ok, something strange is going on. I can't seem to copy it normally. It goes like this in my code:


#5

Almost perfect code! :smile:

Problem is in the semicolon after the for loop in line 21. It simply terminates the whole loop so in line 22 variable i is undefined and this is why you are getting this error.

Delete semicolon and it should work.


Side note, semicolons in line 19 and line 23 are unnecessary, delete them.

Here is a nice article that might help you -> https://www.codecademy.com/blog/1707 :panda_face:


#6

Oh. Thank you so much. How could i overlook it? I already made this mistake once.


#7

You're very welcome!

Well, making mistakes is a very big part of programming. Just try to not make this mistake the third time. :smile: