// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp) === true){
return **myObj[checkProp];**
} else {
return "Not Found";
}
}
// Test your code by modifying these values
checkObj("gift");
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp) === true){
return **myObj.checkProp;**
} else {
return "Not Found";
}
}
// Test your code by modifying these values
checkObj("gift");
I don’t know the language, but it seems to me that the reason 2 doesn’t work is because the line
return myObj.checkProp
is calling the method checkProp of the object myObj - which doesn’t exist. Essentially, myObj is really an array.
So to return the value of the myObj that corresponds to checkProp, you need to return myObj[checkProp]
This is because of the main difference that exists between . notation and the [] notation.
In your code you are passing a variable (“checkProp”).
Since . notation only accept the keys (in your case gift,pet,bed) it takes “checkProp” as a key and check whether “checkProp” exists as a key, thus returns “Not Found”.
However [] notation sees this as a variable and takes the value assigned to the “checkProp” variable, thus returns “pony”.
I hope you are clear.