Basic JavaScript: Testing Objects for Properties help!

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

function checkObj(checkProp) {
// Your Code Here
var check =“”
var result=“”
check = myObj.hasOwnProperty(checkProp);
if (check = true){
result = myObj[checkProp];
return result;
}
else if(check =false){
result = “Not Found”;
return result;
}
return result
}
// Test your code by modifying these values
checkObj(“gift”);

why is my code not working?

One issue seems to be, you are using “=” in place of == for conditional if else comparison.

Why are you declaring check as an empty string and then storing a boolean value in it?

Try something like this:
function checkObj(checkProp) {
// Your Code Here
if(myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];
}
return “Not Found”;
}

2 Likes