5. Finding that Special Someone - Question


#1

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() {
for (var i = 0; i < contactsLength.length; i++) {
printPerson(contacts[i]);
}
}

/*Create a search function
then call it passing "Jones"*/
function search(lastName)
{
for (var i = 0; i < contacts.length; i++) {
if (lastName === contacts[i].lastName) {
printPerson(contacts[i]);
}
else {
console.log("No contact found");
}

}

}
search("Jones");

//So my code keeps printing the else statement as well and I cant seem to find out why :confused:


#2

When your for loop runs, it will compare every name it goes through. So, when it goes through, and reads the other last name(s) too, it will print the else statement for those last names, and the if statement for the one you put in as the parameter.