Solved!Stuck: Testing Objects for Properties

Tell us what’s happening:
I’ve tried many different versions of this code for days. I’ve looked at various sites to try to harden my knowledge on this subject. I’m still confused. I am back with a bit of determination to get help. I have not been able to get this to work, and I don’t have anyone immediately here to help. I can’t give up, but I’ve been stumped here.

Your code so far


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

function checkObj(checkProp) {
  // Your Code Here
  myObj.hasOwnProperty(checkProp);
  return checkObj(checkProp);
  //return "Not Found";
}

// Test your code by modifying these values
checkObj("gift");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties

return checkObj(checkProp);

This line is the problem. You aren’t returning the result of checking if the object has a property, but another function call checking it, which will return another function checking it, which will return another function checking it, and so on for.

If instead you return myObj.hasOwnProperty(checkProp), then it should all work - that evaluates to a boolean value.

1 Like

Thanks. I found a working solution else where, but I want to go over why it wasn’t working to better understand this and prevent something so simple from happening again.

Since checkObj is a function, we don’t call this function within the function, as that’s too much recursion.
Instead, we call the var object myObj to point to what we are using the function on, with the given property checkProp to use as a dummy variable to pass through the object, then grab the need variables and pass it to the return statement.

Please correct me if I’m missing something.