Finding that special someone


#1


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


I don't understand why it logs me both names. Can someone please explain me ?


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 (g = 0; g < contactsLength; g++) {
		if (search === lastName) {
		    printPerson(contacts[g])}
    }
}

list ()
search("Jones");


#2

your list function logs all contacts to the console, that is the purpose of the function?


#3

I had understand that the search ("Jones") was supposed to log just Bob, which is why I don't understand it includes Mary as well.


#4

because its not the search function (which is not coded correctly) tjat causes output, its the list function


#5

Oh, right, I understand. Thank you very much


#6

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