Challenge code showing error

Tell us what’s happening:

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

should return the string

Not Found

.

  **Your code so far**

function checkObj(obj, checkProp) {
// Only change code below this line

var result;
  obj = {
    gift: "pony",
    pet: "kitten",
    bed: "sleigh",
    city: "Seattle",
  }

if(obj.hasOwnProperty(checkProp)){
  result = obj[checkProp];
} else {

  
  result = "Not Found";
}
return result;
// Only change code above this line
}
checkObj({pet: "kitten", bed: "sleigh"}, "gift");

  **Your browser information:**

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

Challenge: Testing Objects for Properties

Link to the challenge:

Why did you overwrite obj with your own hardcoded value? This makes the function argument obj pointless.

2 Likes

Doesn’t it show that whatever value is passed to argument will be equal to the argument wherever used? if I don’t make an object with the same name or assign it to something else how will I pass my value to the argument? Will the function know what to relate to without me telling it?

Hi @muhamidrao !

Welcome to the forum!

These are all good questions.

Here is why you don’t need to create your own object.

Your goal is to create a function that will test any object.
Not just one you created yourself.

Inside the function, the only code you need to add is the if/else statement.
If the object has a specific property, then we will return that property’s value.
Otherwise, we will return “Not Found”

When you call the function, that is where you will pass in the actual object.

Here is an example function call.

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

The computer is only looking at this object from the function call.

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

Then it is checking if "gift" is a property inside that object.
In this case, it is a property so the function would return pony

What the computer is doing, is it is checking the object that is being used in the function call.

There is no database of thousands of objects that the computer is going and checking through.

The computer is only checking that object in the function call.

That is why you don’t need to create your own object.
We want our function to work for hundreds of objects and hundreds of function calls.
Not just one you created.

Hope that clears it up! :grinning:

1 Like

Thank You. That clears up many things.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.