So I had a lot of difficulty with this problem. I had to eventually look up the answer. Here it is:
//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){
// Only change code below this line
for (var x = 0; x < contacts.length; x++){
if (contacts[x].firstName === firstName) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("", "likes");
Basically, I went through the answer and realized my mistakes and how most of it was solved. However, I’m still not understanding how this code actually returns the firstName since the return doesn’t mention firstName (return contacts[prop]) I also don’t understand how the var “x” works in the return since wouldn’t returning"x" just give you the number from the array you found the matching firstName from. So I just need a little bit of clarification how it’s able to return the firstName that’s placed in the function.
EDIT: Nvm, I just realized the problem never asks you to return the name but just to search it.