Whenever a function encounters a return it stops the function and gives you whatever is to the right of the return. What happens when your function hits the first contact in the contacts list?
Okay, lets follow the logic here, okay?
let’s pass in “Akira” and “likes” per the default.
we start a loop. first stop: i === 0, "Akira" === contacts[i].firstName
Akira has the property “likes” it returns our array of likes. So far so good.
Now, let’s pass in “Harry” and “likes”.
We start the loop, now follow the logic…
What happens when i === 0?
Yeah, and what statement is to be executed when the condition is false?
function lookUpProfile(name, prop) {
// Only change code below this line
for (var i = 0;i<contacts.length;i++) {
if (name == contacts[i].firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else if (contacts[i].hasOwnProperty(prop)==false) {
return "No Such Property";
}
} else {
return "No Such Contact";
}
}
// Only change code above this line
}
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0;i < contacts.length; i++){
// nested if statement
if ( name == contacts[i].firstName){
if(contacts[i].hasOwnProperty(prop)===true){
return contacts[i][prop];
}else if (contacts[i].hasOwnProperty(prop)===false){
return "No Such Property";
}
}
// end of nested if statement
} // end of for loop
return "No Such Contact";
// Only change code above this line
} // end of lookupprofile function
// Change these values to test your function
lookUpProfile("Akira", "likes");
One thing that may also give you troubles (I have not checked anything else at the moment) is that you capitalise everything in the string in the return statements, but you need to have them match perfectly with the ones provided in the challenge text "No such property" and "No such contact"
At first glance it doesn’t seem to be anything else