5. Finding that special someone


#1


Error code: TypeError: Cannot read property 'firstName' of undefined

I ran this code through a JavaScript interpreter on another site and it gave me no errors


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 < contacts.length; i++);
    printPerson(contacts[i]);
    if (lastName === contacts[i].lastName) {
        printPerson(contacts[i]);
    }else {
        console.log("Name not found");
    };
};

search("Jones");


#2

Hi, you need to create a variable named search for the function, and also the for loop syntax is off. Try this:

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


#4