function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++) {
if (name != contacts[i].firstName || name != contacts[i].lastName) {
return “No such contact”;
} else if(contacts[i].hasOwnProperty(prop) === false) {
return “No such property”;
} else {
return contacts[i][prop];
}
}
// Only change code above this line
}
This is my first contact with programming of any kind. But I tried to do a different way than the hints page for this prompt. However I can only get “No such contact” to work with what I’ve written. Can anyone help me out and tell me what part of what I wrote is wrong?
function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++) {
if (name != contacts[i].firstName || name != contacts[i].lastName) {
return "No such contact";
} else if (contacts[i].hasOwnProperty(prop) === false) {
return "No such property";
} else {
return contacts[i][prop];
}
}
// Only change code above this line
}
Can you see that no matter which branch of the if/else you go down, you return from the function?
Your issue’s not with ||, it’s with when to return “No such contact”.
Right now the flow of your logic looks like this:
check contacts[0]
if name doesn't match:
return "No such contact"
else if contact doesn't have prop:
return "No such property"
else:
return value of prop in contact
check contacts[1] <-- Your code never gets here
Since you’re guaranteed to return within any of the if/else branches of the first loop, you’re never going to check any contact beyond the first. You can’t say there’s “No such contact” until you’ve checked every contact to confirm there’s no match.