Tell us what’s happening:
My code fails for the first three question and i cant figure out why. I looked at the hints answers and i get why that works and i feel its essentially the same thing as mine, just worded differently.
It failed for these:
“Kristian”, “lastName” should return “Vos”
“Sherlock”, “likes” should return [“Intriguing Cases”, “Violin”]
“Harry”,“likes” should return an array
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++){
if(name == contacts[i].firstName && contacts[i].hasOwnProperty(prop)){
return contacts[i].prop;
}
else if(name == contacts[i].firstName && !contacts[i].hasOwnProperty(prop)){
return "No such property";
}else if(name!=contacts[i]["firstName"]) return "No such contact";
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Kristian", "lastName");
lookUpProfile("Sherlock", "likes");
lookUpProfile("Harry", "likes");
lookUpProfile("Bob", "number");
lookUpProfile("Bob", "potato");
lookUpProfile("Akira", "address");
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup