Struggling to understand the Profile Lookup challenge.
//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 i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else {
return "No such property";
}
}
return "No such contact";
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
This is how I’m understanding how I wrote my code. It checks if the name is in the array If yes, then it checks if it has that property. If yes to the 2nd condition, then it’ll return the value of the property. If false to the 2nd condition, it’ll return “No such property”.
If right away, there is no first name in the array, then it should say “No such contact”.
I can pass the challenge if I move the close bracket above the last return statement (essentially closing out the for loop), but then I don’t understand how that works, but my current bracket placement doesn’t.