var foo = {answer: 42};
if (foo.hasOwnProperty('answer')) {
alert("Wowzers!");
}
Spotted it? hasOwnProperty needs the name of the property to check if the object has it or not. Also, you don’t need to compare the result with true, because if already checks if the result is truthy.
I have tried to change it using the guideline you have provided and now it returns checkProp and no longer returns “Not Found”.
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// Your Code Here\\
if (myObj.hasOwnProperty('gift')) {
return checkProp;
}
else return "Not Found";
}
// Test your code by modifying these values
checkObj("house");
It returns “house” in this case.
What am I missing here? This has been my most frustrating challenge yet
Yes. But if I input “gift”, it should return “pony” and it is not. That is where my frustration lies. My return is not producing what I want to to return and I feel that I am missing a vital step but cannot pinpoint that it is at this moment.
Hey guys. Thank you for your contribution on that topic. It helped me spot what I am doing wrong, but now the question is: Why the dot notation to access objet properties will not work here? Here is my code:
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp)) {
return myObj.checkProp;
}
return "Not Found";
}
// Test your code by modifying these values
checkObj("gift");
If I change it to bracket notation it will work, i mean this bit:
if (myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp];
}