[Solved] Finding that Special Someone


#1


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


I got it to print out the last names, but the error occurred when I tried to see if the param equaled the last name.


Solved:

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 = 0
for (var i = 0; i < contacts.length; i++)
    if (lastName===contacts[i].lastName){
    console.log(contacts[i].firstName+' '+contacts[i].lastName)}
    };
    
  search("Jones")


#2

Here you have a problem, because you put object, but function expect string value:

search("Jones");

Note: you need to put last name.


#3

Thanks. I had to modify the code so it came out as the first and last name, but thanks for pointing out the mistake


#4

For this goal you can use function printPerson:

if (lastName===contacts[i].lastName){
    printPerson(contacts[i]);
}

Don't Repeat Yourself


#5