I am working on the “profile lookup” exercise and I am stuck. While there is no error in the code, I am still not getting through. Will appreciate your help:
`function lookUp(firstName, prop){
// Only change code below this line
for (i = 0; i < contacts.length; i++){
if ((contacts[i].firstName == firstName)&&contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else if (contacts[i].hasOwnProperty(prop) === null ){
return "No such property";
}
else result = “No such contact”;
}
return result;
}
// Only change code above this line
// Change these values to test your function
lookUp(“Akira”, “likes”);
contacts[i].hasOwnProperty(prop) is true or false, but never null. And “No such property” should only be returned if the name matches, but the property does not exist (so maybe have two ifs, one on matching name, and inside it another one on the property).
Also, better return “No such contact” instead of some arbitrary result variable.
Awesome, had the same error and fixed it with your corrections, thanks so much guys!:
function lookUpProfile(firstName, prop){
// Only change code below this line
for (i = 0; i < contacts.length; i++){
if ((contacts[i].firstName == firstName)&&contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else if (contacts[i].hasOwnProperty(prop) === false ){
return “No such property”;
}
else result = “No such contact”;
}
return result ;
}
// Only change code above this line
// Change these values to test your function
lookUpProfile(“Akira”, “address”);