We are all Jones?


#1


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


My code doesn't act "incorrectly" since it says I produced the correct solution. I'm just wondering why Mary Johnson also comes up. If I only searched for Jones, then Bob Jones should be the only name that pulls up right?

If someone could help me figure out how to write my code so only Bob Jones comes up or as to why both names in my directory came up. Or if Mary is just Bob's cousin and I just missed the memo.


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 (i = 0; i <= contactsLength; i++) {
        if (search === lastName) {
           printPerson(contacts(i));
        }
    }
}
list ();
search ("Jones")


#2

the output in the output window is actually from the list function. Your search function doesn't work correctly, it shouldn't have passed


#3

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


#4

#5

@pyplayer11675, here:

search === lastName

search is a function, a function can't equal a string (lastName)

we have a loop to loop over contacts we should see if the current contact lastName equals lastName (function parameter, your search query)

so, i is an integer which matches index of your contacts array. So we can use this for current contact

hope this helps