I have used hasOwnProperty() for this for the first time,
but I have been revising material lately and have run into quite a strange solution. Can someone
explain to me how I am getting “No such property” output, please? Am I right in my guess (in comments)?
Thanks in advance
//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(firstName, prop){
for(var i = 0; i<contacts.length; i++){
if(contacts[i].firstName == firstName){
if(contacts[i][prop]==firstName[prop]){
return "No such property";/*How does it parses as correct, does it return "No such property"
only if (contacts[i][prop]==firstName[prop]) evaluetes to false, skipping it otherwise? */
}
return contacts[i][prop];
}// nested if part ends returning props array
}//for part ends
return "No such contact";// Last output, because for loop has to parse all contacts parts before output it
}
// Change these values to test your function
lookUpProfile("Harry", "likes");