Tell us what’s happening:
The only thing I don’t get is why the return statement for no such contact is outside of the function body. Every time I returned it inside of the function my results wouldn’t be correct… What am i missing?
Your code so far
//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(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
//check if name is a firstName and the prop is a property of that contact
//if both are true, return the value of that property
if (name === contacts[i]['firstName']) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return 'No such property';
}
}
//if name does not correspond to any contact, return "No such contact"
}
return 'No such contact'
//if prop doesnt correspond to any properties found to match name, then return "No such property."
}
// Change these values to test your function
console.log(lookUpProfile("Akira", "likes"));
console.log(lookUpProfile("Tyler", "likes"));
console.log(lookUpProfile("Harry", "lastName"));
console.log(lookUpProfile("Kristian", "plays"));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
.