Hi all.
I am running on a problem with profile lookup challenge.
Here the working solution:
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
if (contacts[i].hasOwnProperty(prop) === true) {
return contacts[i][prop];
}
}
else if (contacts[i].hasOwnProperty(prop) === false) {
return "No such property";
}
} return "No such contact";
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
ALL GREEN!!! yuuhuu
here what I would write:
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
if (contacts[i].hasOwnProperty(prop) === true) {
return contacts[i][prop];
}
}
else if (contacts[i].hasOwnProperty(prop) === false) {
return "No such property";
}
else if (contacts[i].firstName !== firstName) {
return "No such contact";
}
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
“Kristian”, “lastName” should return “Vos”
“Sherlock”, “likes” should return [“Intriguing Cases”, “Violin”]
“Harry”,“likes” should return an array
WRONG!!
What I can’t understand is why the return result for “No such contact”; must lives outside the for loop.
Thank you if you’ll give me an explanation
have fun and code safe