Basic JavaScript - Testing Objects for Properties

I can’t finish this exercise I understand the concept but I can’t translate it into code I don’t understand what the object is and whether it needs to be specified in this exercise and then insert all the parameters to then have them searched by the method, finally I don’t understand or I don’t remember what should I write on the final return

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

The first checkForProperty function call returns true, while the second returns false.


Modify the function checkObj to test if the object passed to the function parameter obj contains the specific property passed to the function parameter checkProp. If the property passed to checkProp is found on obj, return that property’s value. If not, return Not Found.

Thanks for your help

1 Like
function checkObj(obj, checkProp) {
  // Only change code below this line
  if (obj.hasOwnProperty(obj,checkProp)) {
    return obj[checkProp];
  { else {
    return "not found"; 
  }
}
return "";
  // Only change code above this line
}
}

Your formatting makes it a bit hard to see the issue. Look above and count the { and }

1 Like

Also, hasOwnProperty only takes a single argument, the property. It is called on the object and passed the property.

2 Likes

In addition to @JeremyLT and @lasjorg suggestions, what is this return doing?
image

1 Like

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