Tell us what’s happening:
My “no such contact” and “no such property” challenges are passing, but the ones checking names & properties and returning relevant value are not. I read the spoiler solution and it checks property existence separately, unlike mine ( i use && to check both FirstName/name matching and property existence in the same line)
What am i missing? Is that it? should i be checking name matching and property existence separately? if so, why?
I don’t get it and i don’t want to just copy solution.
Are you not supposed to check if the SAME object that matches firstName/name attribute has this property? If so, why do it separately?
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+=1){
if (contacts[i]["firstName"] == name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else if (contacts[i]["firstName"] !== name) {
return "No such contact";
}
else if (contacts[i].hasOwnProperty(prop) == false){
return "No such property";
}
}
}
/* basically, tell code to go through every one of these four. If during any of the 4 iterations it's going to make it finds an object(contact) that has firstName value matching our name attribute and has property matching prop attribute, then return value of that property*/
/* if there is no matching name, return "no such contact". If there is name, but there is no property in that object matching prop attribute, return "No such property" */
// Only change code above this line
// Change these values to test your function
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup