Profile Lookup - Issue

Profile Lookup - Issue
0

#1

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


#2

Let’s walk through a test case which is not passing and see what is going on.

lookUpProfile("Kristian", "lastName"); // should return "Vos"

In your solution above, in the 1st iteration of the for loop, name = “Kristian” and prop = “lastName”, so the first if statement condition evaluates to false, because contacts[0][“firstName”] is “Akira”, so the first condition in the statement is false and both conditions must be true to execute the if statements code block.

So while still in the first iteration, your else if statement checks if contacts[0]{“firstName”] which is still “Akira” is NOT equal to “Kristian”. Well, this condition is true, because “Akira” is NOT equal to “Kristian”, so you return “No such contact”. Remember, when a return statement is executed, the function immediately exists and does not come back to finish a loop.


#3

Thanks so much, i’ll have to read it few more times to wrap my head around it :slight_smile: