Skip to content

Instantly share code, notes, and snippets.

@AndriiBozh
Last active January 1, 2018 12:26
Show Gist options
  • Save AndriiBozh/ce72ea5f0e17a6021bc23fd8aff9865b to your computer and use it in GitHub Desktop.
Save AndriiBozh/ce72ea5f0e17a6021bc23fd8aff9865b to your computer and use it in GitHub Desktop.
Profile Lookup (freeCodeCamp puzzle)
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 (firstName == contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else if (firstName == contacts[i].firstName && !contacts[i].hasOwnProperty(prop)) {
return "No such property";
}
}
return "No such contact";
}
// Change these values to test your function
lookUpProfile("Kristian", "likes");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment