Testing Object for Properties - help

I’m only getting one error saying “gift” should return “Not Found”.
When I delete the property “gift” from the obj object I get an error saying that “gift” should return “pony”. It seems like it’s asking for 2 different answers for the same property and I can’t figure out how to get past this error. Thanks!

I can get each one of these tests to pass but I cannot get both to pass at the same time:

checkObj({pet: "kitten", bed: "sleigh"}, "gift") should return the string Not Found .

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") should return the string pony .

  **Your code so far**

function checkObj(obj, checkProp) {
// Only change code below this line
var obj = {
"gift": "pony",
"pet": "kitten",
"bed": "sleigh",
"city": "Seattle"
}

if (obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
// Only change code above this line
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15

Challenge: Testing Objects for Properties

Link to the challenge:

Question for you. Why did you add this object definition in your function? Do you understand what is happening when you do that?

1 Like

The first test you mentioned passes to the function the object {pet: "kitten", bed: "sleigh"}, which does not have the gift property

The second test you mentioned passes to the function the object {gift: "pony", pet: "kitten", bed: "sleigh"} which has a gift property.

These two tests are there so you can’t hardcode the answer, but you need to use the function parameters without overwriting them

1 Like

I believe it will create a local object and store those values so when someone looks up the property the data is in the obj object. Is this data already stored in the question behind the scenes or something?

You are correct, it creates the obj variable which is local to the function. Notice that it has the same name as the obj parameter for the function. So by creating the local obj you “shadow” the obj being passed into the function.

Which obj are you supposed to be checking properties on?

We can look to the instructions for the answer:

“Modify the function checkObj to test if an object passed to the function ( obj )* contains a specific property”

2 Likes

The function already has the argument obj. This argument is populated when the function is called. Why replace it with a single hard-coded object?

1 Like

Okay I got it to pass thank you! I just needed to put the object outside of the function!

Sort of? You don’t need to create an object at all.

Where is it getting the data from if I don’t create it?

When you or the test suite calls

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

then obj = {pet: "kitten", bed: "sleigh"} and checkProp = "gift".

When you or the test suite calls

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

then obj = {gift: "pony", pet: "kitten", bed: "sleigh"} and checkProp = "gift".

1 Like

OHHHHHHHHH I see. The function itself is creating the object with all of the stuff contained in the first argument. The next argument is the property to be checked. My statement checks if obj contains a certain property and if so returns the value! If not then Not Found. Do I have a good grasp on it?

1 Like

The function call doesn’t quite create the object. Its more than the function call binds the object to the variable name we chose for the argument.

In the example from the test suite, the object only exists inside of the scope of the function call, but that isn’t a requirement.

This syntax also works just fine:

const thisIsAnObjectThingy = {pet: "kitten", bed: "sleigh"};
const hereIsMyProp = "gift";
checkObj(thisIsAnObjectThingy, hereIsMyProp);
1 Like

I see! Thank you for the quick response… this is the first time I’ve used the forum and it’s pretty awesome! Thanks for your help.

3 Likes