When i add just the first -if- condition and check if it works it says the code is just fine and checks the top 3 tests. However, after i add the second part of the code, starting from -else if- for some reason it unchecks the top 3 tests and checks the bottom 3. I don’t understand what’s going on?
Your code so far
// 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(var i = 0; i<contacts.length; i++){
if(contacts[i]['firstName'] === name && contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
else if(contacts[i]['firstName'] !== name){
return 'No such contact';
}
else {
return 'No such property';
}
}
}
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0
.
Challenge: Profile Lookup
Link to the challenge: