Your code is relatively hard to read at first glance, plus writing lots of if else
statement is generally not considered as a great idea because it may lead you to make more mistakes.
(the more you write the more chance you have to make mistakes)
In conditions like this is often considered a good practice to use “early returns”.
There’s a guide on the forum explaining it that I’ll link at the end of the post.
In short the same logic you’ve used can simply be skimmed down to (in pseudo-code) as:
function lookupProfile() {
loop {
if contact.firstname === firstname {
if contact hasOwnProp {
return contact[prop]
}
// here we can execute a return assuming that the prop is not here
// since the previous statement has not been executed
return no property
}
}
// same here, we can return no contact because if we reached this far means
// the contact is not in the list
return no contact
}
Hope makes sense