I’ve completed this exercise, but I’m left with one question.
This code does work:
function lookUpProfile(name, prop) {
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return 'No such property';
}
}
}
return 'No such contact';
}
This code does not work:
function lookUpProfile(name, prop) {
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else if (contacts[i].firstName === name) {
return 'No such property';
} else {
return 'No such contact';
}
}
}
The only difference that seems important, at least to my novice eyes, is the return 'No such contact';
statement being outside the for loop in the code block that passes, but I don’t understand why that makes a difference.
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36
Challenge: Profile Lookup
Link to the challenge: