Javascript Basics "Profile Lookup" - I have set up everything but it doesn't output anything

Javascript Basics "Profile Lookup" - I have set up everything but it doesn't output anything
0

#1

Hi guys,

I’m doing the Profile Lookup Function right now. Where you have to find contacts in an json object.
Here is my code so far:

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

In the screenshot you can see the output (none) and the test cases (just 2 passes).
2017-09-19 18_00_17-Profile Lookup _ freeCodeCamp

Why are the other tests not passing? In my mind that function makes perfect sense. What did I do wrong?

Thanks guys!


#2

try accessing the object using bracket noation instead of dot return contacts[i][prop] .when you use dot, javascript interprets prop as text instead of variable.


#3

Yes that’s it thank you.
I was just wondering because FCC Editor gave a warning “is better written in dot notation”.
Anyway it’s working now thx :slight_smile: