I just finished this problem afters spending all day on it, and I wanted to know if I solved this in a good way. I know the code works, but I feel like I wrote way more code than was necessary, and would like some input from someone more experienced.
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){
// Only change code below this line
var name = "";
var hasProperty = false;
for( var i = 0; i < contacts.length; i++) {
if(contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
if(contacts[i].firstName === firstName) {
name = firstName;
}
if(contacts[i].hasOwnProperty(prop)){
hasProperty = true;
}
}
if(name !== firstName) {
console.log("No such contact");
return "No such contact";
}
if(hasProperty === false) {
return "No such property";
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("jake", "funny");