Hello mates, I can’t understand why my solution will result in ‘no such a contact’.
//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
for (let i = 0; i < contacts.length; i++){
var contact = contacts[i]; //contact is now an object
if ( contact.firstName === name ){
if ( contact.hasOwnProperty(prop) ){
return contact[prop];
} else {
return 'No such property';
}
} else {
return 'No such contact';
}
}
// Only change code above this line
}
I Looked at the solution, and it seems that the last return statement (’’'return 'No such a contact```) must be placed out of the for loop. But why? Should not a return statement stop and exit the function (and so the for loop too?)
For example, in the first if, if name is not equal to the firstName, then it should execute the outer else branch which returns ‘No such contact’. That what happened even when the firstName is equal to name…but why? Any help? thank you in advance