Basic JavaScript: Testing Objects for Properties help!

Basic JavaScript: Testing Objects for Properties help!
0.0 0

#1

// 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?


#2

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


#3

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”;
}