5. finding that someone special


#1



I'm not sure what part of my code is wrong. I've already looked on this site and my code is the exact same as other who have it work. Thanks for the help in advance.


Oops, try again. It looks like your search function didn't log "Bob Jones" to the console. Did you remember to call search after you defined it?


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"*/
var search = function(lastName) {
    var contactsLength = contacts.length;
    for (var i = 0; i < contactsLength; i++) {
        if (lastName === contacts[i].lastname) {
            printPerson(contacts[i]);
        };
    };
};

search(Jones);


#2

@jrothe22,
Try is by using

contacts[i].lastName

in the comparison in your search =function=


#3

Thanks I appreciate it.


#4

My final code, which works:

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"*/
var search=function(lastName){
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
if (contacts[i].lastName===lastName){
printPerson(contacts[i]);
};
};
};
search ("Jones");


#5

Hi,

I know that you have solved this problem, but, when I first read your code, it looked just liked mine. The only error I saw was that you didn't put Jones in quotes when you were calling your search function.

e.g.

search("Jones");