Do not understand solution with hasOwnProperty()

testing object for properties
Hi all!
Do not understand, why in line 4 to adress the value “pony” of the only matching object in the list - out of 3 - I have to use obj[checkProp]. If I only use “return obj” I get the whole list of the 3 objects. Why is obj[checkProp] not returning all properties of all 3 objects? The filtering is not clear to me. And what if I just wanted to display the property of the matching object gift: “pony” ? Can I adress only its property “gift” somehow? "return obj[property?] " . I can not work with indexes here to address the property.
Thank you so much!

function checkObj(obj, checkProp) {
  // Only change code below this line
  if (obj.hasOwnProperty(checkProp)){
      return obj[checkProp];   // <- do not understand this
     }
  else{
    return "Not Found";
  }
  // Only change code above this line
}

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift");

The function parameters only have one value per function call

Hm I do not get it. All 3 value-pairs are passed to the function at the same time as arguments? Or one after the other and processed like an iteration?
The function is only called 1x. This is confusing.

You keep saying there are 3 objects so I think you’re confused about what an object is. An object is a collection of key-value pairs.

{
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
}

This is one object, with three key-value pairs.

1 Like

The tests make many function calls

I had some misunderstandings regarding the whole object topic , so I repeated it. I guess I understand now better. Thank you all!

In run time the object is passed to the function checkObj, parameter, obj. The property in question is passed to the function, checkObj, parameter, checkProp. So if obj.hasOwnProperty (checkProp) is true obj[checkProp] will return the associated value with the property in question of the object that was passed into the parameter, obj.
So in line 4 that is calling ‘pony’ from the value passed into value associated with the property “gift”. It might be confusing to look at because gift is passed as a string but that is for the computer to recognize it as a data type that can be compared with the properties in the object. As someone mentioned before. They are key value pairs, such that gift is the key and “pony” is the corresponding value.

1 Like

function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.hasOwnProperty(checkProp)){
return obj[checkProp];
}
else{
return “Not Found”;
}

Hey everyone
Can someone please help me understand why we had to use obj[checkProp] instead of just return checkProp furthermore why can’t we use dot notation like this "obj.checkProp?

Hi @WolfKissed,

If you need help, please use the Help option from the challenge itself after you’ve tried your code a few times. I’m closing this topic now.

Btw, I’ve PM’d you about this so that you know I’m not just dismissing what you’re asking about.