Testing Objects for Properties - tried several suggested codes but not passing

Tell us what’s happening:

Hello,

I have spent a number of hours trying to pass this test. This includes trying several suggested codes in the “get a hint” section, but to no avail. I would seriously appreciate any help. Thanks!

Steve

Your code so far

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

function checkObj(checkProp) {
  // Your Code Here
  function checkObj(checkProp)
  if (myObject.hasOwnProperty(checkProp)){
    return myObj[checkObj];}
  
  else {return "Not Found";}
  }
  return "Change Me!";
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/testing-objects-for-properties

The only thing your function ever returns is "Change Me!".

Also, there is no good reason to have a checkObj function inside your checkObj function.

checkObj("gift") calls the checkObj() function and once inside that function, you have another function with your condition that is not being called. So like @ArielLeslie said, you only will ever get “Change Me!” no matter what you throw at it.

I can see where you’re going with the logic here and it looks solid enough but I think you need to look at a few things.

  • You seem to have an extra curly brace somewhere in your code.
  • There’s one line repeated that needs some attention too.
  • You should check the variable names you’re using in your function.
  • Your inconsistent style ( brackets, new lines, etc) make your code hard to follow. You might want to use an online code beautifier or linter to help with style and errors.
function checkObj(checkProp) {
  // Your Code Here
  function checkObj(checkProp)  //why this line repeated?
  if (myObject.hasOwnProperty(checkProp)){  //do you have an object name myObject?
    return myObj[checkObj];}  //what is checkObj?  is it a property name or something else

  else {return "Not Found";}
  }
  return "Change Me!";
}

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

Good luck!

1 Like