Testing Object for Properties

So I came very close to solving this challenge, but gave up and looked at the answer my original code looked like this

function checkObj(obj, checkProp) {
  // Only change code below this line
  if (obj.hasOwnProperty(checkProp)){
    return obj;
  }else
  return "Not Found";
  // Only change code above this line
}

however the actual answer was this

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
}

I’m just curious why

return.obj;

isn’t valid and why it needs to be

return.obj[checkProp];

because .obj has already cleared the condition in my if statement of containing checkProp it seems to me returning .obj is the same as returning .obj[checkProp].

Because the return value for the function should either be “Not Found” if the property isn’t found or the property’s value. Returning obj returns the entire object. You want to return the value of the property (checkProp) for the object, which you access with obj[checkProp].

Also, I noticed you kept putting a dot before obj in your explanation. Was there a reason for that?

1 Like

Ahh thank you for the explanation, and no I just made a typo I think.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.