Finding that special someone


#1


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


I'm not sure what I'm doing wrong.


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() { 
    var contactsLength = contacts.length; 
    for (var i = 0; i < contactsLength; i++) { 
        if (contacts.lastName === lastName) { 
            return printPerson(contacts[i])
        } else { 
            return contacts
        }; 
    }; 
};
console.log(search);


#2

Ok firstly you need to set your function with a parameter. The parameter used in this case is lastName. This allows you to define what you are searching for!

function search (lastName) {

Next we have the issue of declaring the if statement;

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

This will never work because contacts.lastName is not defined.
You have an array (contacts) which includes items which have the property of lastName, but unless the object you are selecting is declared (in this case Bob or Mary (or a number value selecting them)) then it can't read the value of the property.

        if (contacts[i].lastName === lastName) {    //if bob or mary.lastname = param
        printPerson(contacts[i]);                   //printperson
        }

Finally you then need to call the newly created function;

search('Jones');

Hope this cleared things up for you!


#3

Yes it did!! Thank you so much


#4

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