Finding that Special Someone


#1

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

Oops, try again. You have the following error in your code: TypeError: Cannot read property 'lastName' of undefined

This is what's being logged to the console:

Bob Jones
TypeError: Cannot read property 'lastName' 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 (g=0; g <= contactsLength; g++) {
        if (contacts[g].lastName === lastName) {
            printPerson(contacts[g]);
        }
    }
}

search("Jones");


#2

I think you need to change your last if statement and also call your list function.

You last if should look like this.

if (search === lastName) {
            printPerson(contacts(g)); }
    }
}
list()
search("Jones")

If this helped please mark my reply as the solution.


#3

Hey I did just that and it worked.

Thanks for the help


#4

All good, anytime :slight_smile:


#5

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