My goal:
I am trying to create a function that will check if the firstName provided matches an existing contact's firstName in the contacts array, and if the prop specified is a valid property of that contact.
If both conditions are met, the function should return the value of that property.
If the firstName does not match any contacts, the function should return "No such contact".
If the prop does not correspond to a valid property, the function should return "No such property".
This is my code:
//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){
// Start function
for (var i = 0; i<contacts.length; i++){
if(contacts[i].hasOwnProperty("firstName") === true && contacts[i].hasOwnProperty(prop) === true){
return contacts[i][prop];
}
else if(firstName !== contacts[i].firstName) {
return "No such contact";
}
else if(!contacts[i].hasOwnProperty(prop)){
return "No such property";
}
}
// End function
}
lookUpProfile("Akira", "likes");