Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["JavaScript", "Gaming", "Foxes"]
}
];
My solution
function lookUpProfile(name, prop) {
for (const property in contacts) {
if (contacts[property].firstName === name && contacts[property].hasOwnProperty(prop)) {
return contacts[property][prop];
} else {
return `No such property`;
}
}
}
For some reason, it’s always returning “No such property” even if I put arguments that pass the test. If I remove the return
in the else
clause it works as intended.
I tried to create a variable to assign the results so I could return it outside the loop but it didn’t work.
I appreciate any help!