The code with the if else statement, so you can see what happens
Try with maybe these function calls, and see what your code does:
lookUpProfile("Sherlock", "number"); // should return "0487345643"
lookUpProfile("Jacob", "likes"); // should return "No such contact"
lookUpProfile("Kristian", "address"); // should return "No such property"
Like Randell already mentioned. The return statement kicks you out of the loop. You can fix it by introducing a variable above the loop, assign the matching value to it inside the loop and return the variable after the loop. Something like this:
//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){
// Only change code below this line
var match;
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name && contacts[i][prop]) {
match = contacts[i][prop];
} else if (contacts[i].firstName !== name) {
match = "No such contanct"
} else {
match = "No such property";
}
}
return match;
// Only change code above this line
}
// Change these values to test your function
console.log(lookUpProfile("Kristian", "likes"))