I am working on a project of mine where I have this contact list contacts which is an array containing objects.
The problem is that my function searchPerson always returns the response for the condition of the searched for person not existing.
As soon as I remove the condition and call the function again it returns what was searched for.
I don't understand why with the condition it always return no such person found! when that person exists in the contact list! Can anyone help me understand why this is happening?
This is my code.
var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "[email protected]"
};
var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "[email protected]"
};
//Here we populate ou array.
var contacts = [bob, mary];
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}
function searchPerson (lastName) {
var contactsLength = contacts.length;
for(var i = 0; i < contactsLength; i++){
//set a condition that we only retrieve the last name if it already exists in out contact list
if(lastName !== contacts[i].lastName){
return console.log("No such person found!");
} else {
printPerson(contacts[i]);
}
}
}
==operator.