0

I'm working through the freeCodeCamp javascript and got stuck on the "profile lookup" exercise because I forgot about the .hasOwnProperty() function, but I am still not sure why my original function did not work. I'm leaving in a portion of the given array for reference.

//Setup
var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    };


function lookUpProfile(name, prop){
// Only change code below this line
for(let x in contacts){
  if(name === contacts[x].firstName){
    for(let y in contacts[x]){
      if(prop === y){
        return contacts[x][prop]; 
      } else {return "No such property";}
    }
  } 
 } return "No such contact";
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes")

When I leave out my

else {return "No such property";}

line it works, but otherwise just returns "No such property" no matter what the 'prop' input is.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114

3 Answers3

1

In your code:

for(let y in contacts[x]){
  if(prop === y){
    return contacts[x][prop]; 
  } else {return "No such property";}
}

If prop is likes, for instance, the first time round the loop y may equal firstName. if ("likes" === "firstName") is false, so we return "No such property";.

As you discovered by removing the else, what you want to do is to test every key, then return "No such property" if you get to the end:

for(let y in contacts[x]){
  if(prop === y){
    return contacts[x][prop]; 
  }
}
return "No such property";

BTW, please use more white space - jamming things together on a single line is much harder to read.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
1

Your issue is your if else statement in the loop:

if(prop === y){
  return contacts[x][prop]; 
} else {
  return "No such property";
}

Based on your code, you only check the first property of your contact objects because your if else statement returns no property if the current property being checked does not match with the one being looked up. This condition makes it so it doesn't check the rest of the properties and only the first one (regardless of it being the right one or not)

To fix this, move your return statement to the end of the loop you use to check the properties:

for(let x in contacts){
  if(name === contacts[x].firstName){
    for(let y in contacts[x]){
      if(prop === y) return contacts[x][prop]; 
    }
    return "No such property";
  }
}

This way, if you finish your loop and you still haven't found a valid property, it will correctly return the "no such property" message and vice versa if you have found the correct property, it will return the property value

Ramenous
  • 218
  • 1
  • 8
0

Just move else part of your if condition in your inner loop out of that loop. Because, code execution is returning right after comparison of first property.

Zeeshan Elahi
  • 247
  • 2
  • 9