Profile Lookup - Error with Bob

Tell us what’s happening:
Hello,
I am getting an error on
"Bob", "potato" should return “No such contact”
even thought the one prior to that with the name Bob is written the same way.

Thanks,

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(firstName, prop){

  var msg = 'No such contact';

  for (var p in contacts) {
    if (contacts[p].firstName === firstName && contacts[p].hasOwnProperty(prop)) {
      msg = contacts[p][prop];
    } else if (!contacts[p].hasOwnProperty(prop)) {
      msg = 'No such property';
    }
  }

  return msg;
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Harry", "likes");
lookUpProfile("Bob","number");
return msg "No such contact"

lookUpProfile("Bob","potato");
return msg "No such contact"

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

else if (!contacts[p].hasOwnProperty(prop)) {
      msg = 'No such property';
    }

This will cause msg to be set to “No such property” even when contacts[p].firstName is not equal to firstName.

So let’s breakdown your lookUpProfile function step by step to see why the msg variable doesn’t end up the way we want it.

function lookUpProfile(firstName, prop){

  var msg = 'No such contact';

We default to msg to “No such contact” which is a good way to go about it.

for (var p in contacts) {

Next, we’re looping through our array of contacts for the purpose of finding the desired contact. Again, this is a solid way to go about the search.

if (contacts[p].firstName === firstName && contacts[p].hasOwnProperty(prop)) {
      msg = contacts[p][prop];
    }

Now we’re checking to see if whichever contact we’re on has both the correct name and the matching property. If they do, we update the msg to return whatever value is being stored in the property. This makes sense and correctly follows the goal of the exercise.

else if (!contacts[p].hasOwnProperty(prop)) {
      msg = 'No such property';
    }

And here, for the final segment of code before the return statement. We decide that if contact failed the previous if statement check, we’ll check to see if they lack the property that we’re searching for. This is where the code’s logic starts to fail.

If the contact does have our desired firstName but not the desired prop, the code will execute fine. But let’s say we search through the entire array and didn’t fine the contact with the firstName “Bob.” On the final pass through the array, we would fail the first if statement and go down to the else if statement. We’re checking to see if the property exists, even though “Bob” isn’t even the contact we’re looking at. If the property doesn’t exist, we update message to say “No such property,” even though the guidelines state we should return that there is no contact to begin with.

So how can we go about fixing this up?

Without going into an explicit answer, I would recommend splitting the first if statement into two distinct ones and nesting one inside the other.

By checking contacts[p].firstName === firstName on its own first, we can determine whether or not it’s even worth checking for the property inside the contact object.

Hopefully this clarifies where the issues were coming from and nudges you in the right direction.

1 Like