Tell us what’s happening:
Once again, I’m having trouble with the logic. First, I wasn’t sure whether to put quotes within the hasOwnProperty function. (method) When I removed the quotes, I kept getting undefined, so I put them back. The return value with quotes is always Not Found.
Your code so far
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
if(myObj.hasOwnProperty("checkProp"))
{
return myObj.checkProp;
}
return "Not Found";
}
// Test your code by modifying these values
checkObj("gift");
// Test your code by modifying these values
checkObj("gift");
/* TEST RESULTS
// running test
checkObj(“gift”) should return “pony”.
checkObj(“pet”) should return “kitten”.
// tests completed
*/ Your browser information:
When you write myObj.hasOwnProperty(“checkProp”), you are asking if myObj has an actual property named “checkProp”. Remember, checkProp is a variable. It contains a string, so you can just reference checkProp without the quotes around it.
You can not use dot notation to access an object’s property with a variable. Review your previous lessons on how to do this properly.
This is what I did, and it worked, but I have a couple of questions:
Is this the best way to do this lesson? It seems like you should be able to put checkProp directly in the brackets since both checkProp, v1, & v2 are all variables. Any explanation for this? Thank you for your help.
Not sure how to answer it other than to say because it doesn’t exist. If js doesn’t find something to return, it usually returns an undefined or something similar.