Last active
January 1, 2018 12:26
-
-
Save AndriiBozh/ce72ea5f0e17a6021bc23fd8aff9865b to your computer and use it in GitHub Desktop.
Profile Lookup (freeCodeCamp puzzle)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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