Testing Objects for Properties! Help

I cannot for the life of me get the string to return “not found” on this problem and I really don’t know what I’m doing wrong. :frowning:

   **Your code so far**

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

function checkObj(checkProp)
{} 
if(myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];
} else if(myObj.hasOwnProperty(checkProp) !== true);
{return "Not Found"; }

 // 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/95.0.4638.69 Safari/537.36

Challenge: Testing Objects for Properties

Link to the challenge:

Why have you hard-coded an object? You need to use the function argument obj.

can you expand on that? I’m very lost here

You created the myObj in your function. That wasn’t part of the instructions. You need to remove that object and instead only refer to the function argument obj


Edit: Ahhh… It looks like you copy-pasted an old answer you found somewhere into your code. That won’t work

Hi @amyjtaytay !

Welcome to the forum!

Here is the reason why you do not want to create an object like this.

You want to write a function that works for ANY object.
Not just one you coded yourself.

You want to create a function that works for hundreds of objects and function calls.
It is not practical to hardcode hundreds of objects. :grinning:

The goal of your function is to check if obj has a property of checkProp.
Then when you call the function you will pass in an actual object to test.

Here are some example function calls.

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

That is the cool thing about functions.
We can use parameters to act as placeholders for the real values. :grinning:

I would reset the lesson and not change the function parameters.
This is what the function looked like before you change it.

function checkObj(obj, checkProp) {
  // Only change code below this line
  return "Change Me!";
  // Only change code above this line
}

Do not touch this part at all

(obj, checkProp) 

Only add code inside the function.

For this part,

you need to check for obj not myObj

Also, since we are dealing with a boolean, you can just use an else statement here

else {
    return "Not Found";
  }

When you think about, either the property exists or it doesn’t.

You don’t have to explicitly say !== true :grinning:

Hope that makes sense!

1 Like

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