"Profile Lookup" — walkthrough?

Link to challenge: https://www.freecodecamp.org/challenges/profile-lookup

I spent a few hours on this but a solution wasn’t clicking. I knew that I had to write an if/else statement but wasn’t sure where to go from there. Could you walk me through how to solve it to help me understand the thinking process behind it? When looking at the solution, I would not have come up with it on my own.

If you’re wondering, my initial solution was:

for (var i = 0; i < contacts.length; i++) {
if(contacts.hasOwnProperty(firstName && prop) {
return contacts[firstName && prop);
} else if (firstName != contacts.firstName) {
return "No such contact";
} else if (prop != contacts.prop) {
return "No such property"; 
}
}

This was my logic behind it. I know now that it’s wrong. The correct solution (which I did not come up with myself) is:

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"]
    }
];

for (var x = 0; x < contacts.length; x++){
    if (contacts[x].firstName === firstName) {
        if (contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
}
return "No such contact";

lookUpProfile("Akira", "likes");
  1. for (var x = 0; x < contacts.length; x++) => The for loop iterates over all the contacts
  2. if (contacts[x].firstName === firstName) => Each iteration the code checks if the current contact has the same firstName as the one we are looking for
  3. if (contacts[x].hasOwnProperty(prop)) => If we found the correct contact (in the previous if statement), then we check if this contact has the property we are looking for
  4. return contacts[x][prop]; => If the contact has the correct property we return the value of that property. Note that by using return we automatically exit the function.
  5. return "No such property"; -> If we found the correct user, but it does not have the correct property, we return “No such property”.
  6. return "No such contact"; => If we found a contact that matches we already returned either the value of the property or “No such property”, with return exiting the function. So, if the for loops end without returning anything, we can be sure that there is “No such contact”.

If you need to find a property of a contact, you will first have to find the contact. That is why you iterate over all contacts untill you find a contact with matching firstNames. Then, if you have the contact you can check if it has the property you want to know. If so, return it. Else, return “No such property”. Since the function will have stopped executing once you found a contact, you can return “No such contact” after the for loop has completed.

5 Likes

Thanks for the explanation and the link!

I understand how the code works now that it’s in front of me, but I don’t think I would have thought to come up with it myself. That seems to be a common problem and probably not something you can learn how to do by having someone tell you. I hope that with time I’ll be able to adapt the kind of thinking these challenges require, so I don’t have to resort to looking at the hint.

Here is my code (which doesn’t work):

function lookUpProfile(firstName, prop){
// Only change code below this line
for (i = 0; i < contacts.length; i++) {
  if (firstName === contacts[i].firstName) {
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else {
      return "No such property";
    }
  } else if (firstName !== contacts[i].firstName) {
    return "No such contact";
  }
}
  

// Only change code above this line
}

I still can’t understand why the return "No such contact" is out of the loop :confused:

1 Like

Mine looks quite similar, but works. The logic behind having the last return value after the loop is finished is that after it goes over each contact, it no longer needs to validate anything, the contact doesn’t exist:
´´´
function lookUpProfile(name, prop){
// Only change code below this line
for(var i=0;i<(contacts.length);i++){
if (contacts[i].firstName===name){
if(contacts[i].hasOwnProperty(prop)) return contacts[i][prop];
return “No such property”;
}
}
return “No such contact”;
// Only change code above this line
}
´´´