Testing objects for properites

// Setup
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
// Your Code Here
if(myObj.hasOwnProperty.checkProp)
{
return myObj.checkProp;
}
else
{
return “Not Found”;
}

}

// Test your code by modifying these values
checkObj(“gift”);

I don’t understand what;s wrong with my code?

Your logic and approach to the problem are spot on, but you are not using the hasOwnProperty correctly.

The syntax should look like --> // myObj.hasOwnProperty(“property you wish to check” ) //

Your conditional is asking whether there is a property nested on a property called "checkProp"
which would look like
var myObj={
hasOwnProperty:{checkProp:“Some Value…” }
}

I hope this was at all helpful, glad to help further if nothing I said was too clear. The instructions to the problem show how to use the the method correctly as well.

That challenge only requires that you return true or false, so you can just do:

return myObj.hasOwnProperty(checkProp);

But, if you want to access the property’s value like your code tries to do, you need to use the bracket notation when accessing the object via a variable, e.g.:

return myObj[checkProp];

// Setup
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
// Your Code Here
if(myObj.hasOwnProperty[checkProp])
{
return myObj[checkProp];
}
else
{
return “Not Found”;
}

}

// Test your code by modifying these values
checkObj(“gift”);

unfotunately, still doesn’t work

The challenge wants you to return a boolean (true/false), so you can just check it with the hasOwnProperty() function, as shown below. I only mentioned the brackets for general reference if you want to access a property when using a variable for the property name.

function checkObj(checkProp) {
    return myObj.hasOwnProperty(checkProp);
}
1 Like

got it lol thankssssssssssssssssssssssssss

so, it has to be written with if/return, else/return correct?