Clarity on hasOwnProperty Plz: Pets and Pony?

Tell us what’s happening: I was confused w/ the task for this section, so I submitted my wrong answer anyway to see what I was missing and got even more confused when references to “checkObj({gift: “pony”, pet: “kitten”, bed: “sleigh”}, “gift”)…should return the string pony.” The task instruction didn’t mentioned anything about pony, pet, kitten etc. I saw some of the solutions posted and need clarity on how the others know pets and pony were 2b added? I must’ve skipped a section or something.

  **Your code so far**

function checkObj(obj, checkProp) {
// Only change code below this line
var checkObj = {
  obj: "checkProp",
return "Change Me!";
// Only change code above this line
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36.

Challenge: Testing Objects for Properties

Link to the challenge:

Hi @LeeNg !

You don’t need to create a object.
I would reset the lesson.

Your task is to create an if/else statement based on if the obj has that given property.

Let’s tackle the structure for the if statement.
if an object passed to the function ( obj ) contains a specific property ( checkProp )

This basically translates to if obj has a property of checkProp
You are going to create an if statement and use the hasOwnProperty.

Inside that if statement you are going to return the property’s value if it is found.
You should use bracket notation to find the value of a property.

Your else block would be to return “Not Found”

These are just the test cases. We want to create a reusable function that works for any object and property arguments.

So when we call the function like this

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

or this

checkObj({city: "Seattle"}, "city")

Then the computer is just going to check if the property exists and return that value otherwise it will just return “Not Found”

Make sense?

1 Like

Thanks for the quick reply! I made some adjustments for testing. Can you tell me what I’m missing? Keep getting Not Found.

// Only change code below this line
  var obj= {
  if(obj.hasOwnProperty(checkProp)) {
    return obj[checkProp];
  } else {
    return "Not Found";
  // Only change code above this line

You are really close.

You need to delete this in order to pass the test.

The reason why you should not hardcode an object like this is because you can’t use this function for other objects.

What if I wanted to pass through another object that was not listed in the test cases.

Or what if I wanted to test 100 different objects?

Then you would have to constantly update your function to include those key value pairs.

Your code should work for any object when I call the function.


Also, when you call the function you need to include both parameters.
This would be incorrect.

These would be proper function calls.

checkObj({gift: "bracelet", pet: "dog"}, "dessert")
checkObj({food: "pizza", car: "ford", movie: "Titanic"}, "food")

You notice that I just made up two objects to check if those properties of dessert and food exist.

If you just have the if else statement in your function that you can test if these properties exist.

But with your current code you would have to back and add those key value pairs here

You want to create a function that can handle 100,200,500 test cases and pass all of them. And not by hardcoding every single value to get them all to pass. That would be exhausting.

That is why we have these two parameters here in the function

function checkObj(obj, checkProp)

So when we call the function it will test an object obj and property name checkProp

Hope that makes sense


Yes, the “proper function call” examples definitely cleared things up. Thx!

1 Like