Tell us what’s happening:
The question called Testing Objects for Properties is loading up with an error that reads- “(both) obj + checkProp is declared but its value is never read.” It also looks different from the video tutorial in that it’s missing the set-up portion where the object is declared along with the properties and their values. I tried resetting the answer multiple times; I’m not having any luck.
Thanks
Your code so far
function checkObj(obj, checkProp) {
// Only change code below this line
return "Change Me!";
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.
function checkObj(obj, checkProp) {
// Only change code below this line
if (checkObj.hasOwnProperty(checkProp)) {
return checkObj[checkProp];
} else {
return "Not Found"
}
// Only change code above this line
}
checkObj is the name of the function, so you wouldn’t be checking that for an object property. You want to check the object passed into the function for the property. You have two arguments passed into the function but you are currently only using one of them. Use both of them.
For this particular function, yes, the first one is an object (hence named obj) and the second one is the property you want to check for in the object (hence named checkProp). But that is not a rule in general for functions. This function could have very easily had checkProp as the first argument and obj as the second.
function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.hasOwnProperty(checkProp)) {
return checkObj.checkProp;
} else {
return "Not Found";
}
// Only change code above this line
}
What exactly are you trying to return in this function? If the property you are checking for (stored in the checkProp variable) does not exist in the object then you return ‘Not Found’. But if it does exist in the object then you want to return the value stored in that property in the object.
So how do you get the value of a property in the object? We know the name of the object is obj and we know the name of the property we are checking for is stored in checkProp. What notation can we use to get the value?
function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.hasOwnProperty(checkProp)) {
return Obj[checkProp];
} else {
return "Not Found";
}
// Only change code above this line
}
You have it correct except that you have a silly typo. I don’t want to ruin the fun and tell you exactly where it is but I will remind you that JS is very case sensitive.