5 TypeError: Cannot read property 'firstName' of undefined


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/building-an-address-book/0/5?curriculum_id=506324b3a7dffd00020bf661#

<In what way does your code behave incorrectly? Include ALL error messages.>
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);
}

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

/Create a search function
then call it passing “Jones”
/
function search(lastName){
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++){
if (lastName == contacts[i].lastName){
printPerson(search[i]);
}
}
};
search(“Jones”);.

<do not remove the three backticks above>

Anyone? Help? <3

#2

You’re calling printPerson(search[i]), while you should be calling printPerson(contacts[i])