Hi,
I’m trying to complete the profile lookup challenge on the javascript course. There is a problem with my first if statement but I don’t understand why? I had a look at the solution which makes sense to me but it uses nested if statements. Please help me understand what I have done wrong!
// 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”]
}
];
function lookUpProfile(name, prop){
for (let x = 0; x < contacts.length; x++) {
if ((contacts[x].firstName === name) && (contacts[x].hasOwnProperty(prop))) {
return contacts[x][prop];
} else if ((contacts[x].firstName === name) && (!contacts[x].hasOwnProperty(prop))) {
return "No such property";
} else if (contacts[x].firstName != name) {
return "No such contact";
}
}
}
lookUpProfile("Akira", "likes");
Challenge: Profile Lookup
Link to the challenge: