Profile Lookup - Passing w/o hasOwnProperty?

Hi all, so I’ve passed this test, but after checking the solution I noticed that they used hasOwnProperty in the nested if statement. And that makes total sense now that I see it. I guess I’m a little confused now as to why mine works. Let me try to explain a few steps and tell me if I’m understanding it right, because objects and arrays are still pretty new and confusing to me. Sorry it’s going to be a bit long-winded, but writing it out like this will help me understand I think.

So first of all, dot notation like .prop is used to access objects, and brackets are used to access arrays, correct?

So in my first if statement, I’m checking the object in the contacts array at location contact[i] that has the object property .firstName. Ok, I think I got that.

Now in the second if statement my thought process was: I want to check if the prop in the function is actually a property of the object in location contacts[i]. If true, return the values. If not, we move on. So I first tried to use dot notation: contacts[i].prop because my thinking was that it would take the value being searched for (so if we’re searching for “number”, for example, the if statement would check to see if contacts[i].number exists as a property of that object). But this didn’t work. So now I’m thinking it’s because prop isn’t being replaced here, instead the if statement is actually looking for a property named prop in that position. Is this true?

So now looking at my answer of using [prop], the bracket notation signifies that it should be looking at the values of prop, and since every passing property condition in the challenge actually contains values, my if statement always returns true when it should, and false when it should.

Ok I think that’s it, now can someone tell me if I’m thinking about this correctly? :smiley:

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][prop]) {   //solution: if (contacts[x].hasOwnProperty(prop))
      return contacts[i][prop];
    }
    else {
      return "No such property";
    }
  }
}
  return "No such contact";

Your if statement requires contacts[i][prop] to by truthy. If contacts[i] doesn’t have the property then it is undefined. undefined is falsey. Where your solution would differ from one using hasOwnProperty is if the value of the property was a falsey value (like 0).

In this case there is no difference, but your way wouldn’t work if the property
had a falsy value, like 0, undefined etc. Because then even if the property existed your contacts[i][prop] would be evaluated as false and would give incorrectly that the object doesn’t have the property.

Now, to your queries:

Not correct, dot notation check for the exact name of the property, bracket notation first evaluates what’s inside the brackets. So bracket notation is used if you have a variable containing the property key.

Your reasoning after that is correct.

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

Let me explain what i think you’re asking.

To access an array you must use the bracket notation ([ ]) to look in the index .

But, to access an object property you can use either

  • Dot notation ( . ) to access a knowed property that actually will be in the code (hardcoded)
//E.g. 1
let myObject = {
name: "Lucky",
animal: "Cat"
}
myObject.name // Lucky
  • Bracket notation to access a non knowed property. That is because JavaScript will evaluate whatever is inside the brackets and will try to convert it into a string and will look for a property on the object named like that.
//E.g. 2
let myObject = {
name: "Lucky",
animal: "Cat"
}
let unknowedProperty = "name";
myObject[unknowedProperty] //Lucky

unknowedProperty = "animal";
myObject[unknowedProperty] //Cat

They both will look for a property in the object, but if they don’t find any property named like that they will start to find in the prototype chain. That’s why they use the method hasOwnProperty(). Using this method JavaScript will only look for the name of the property you passes in the method in the actual object, not in its prototipe chain.

I recommend to you to read about the prototype chain. It’s a very important topic in the JavaScript road.

@ArielLeslie @ieahleen @JScars, thank you guys for the answers! Getting multiple perspectives is always helpful in the learning process. Looks like I have some reading up to do!