I'm working through the freeCodeCamp javascript and got stuck on the "profile lookup" exercise because I forgot about the .hasOwnProperty() function, but I am still not sure why my original function did not work. I'm leaving in a portion of the given array for reference.
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
};
function lookUpProfile(name, prop){
// Only change code below this line
for(let x in contacts){
if(name === contacts[x].firstName){
for(let y in contacts[x]){
if(prop === y){
return contacts[x][prop];
} else {return "No such property";}
}
}
} return "No such contact";
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes")
When I leave out my
else {return "No such property";}
line it works, but otherwise just returns "No such property" no matter what the 'prop' input is.