I'm stuck in Testing Objects for Properties

Hi, it’s my first time to post a question. I’m stuck on how to get “Not Found” result in checkObj({pet: “kitten”, bed: “sleigh”}, “gift”). Should I create a new variables?

function checkObj(obj, checkProp) {
  // Only change code below this line
  var obj = {
    gift: "pony",
    pet: "kitten",
    bed: "sleigh",
    city: "Seattle" 
    if (obj.hasOwnProperty(checkProp)) {
       return obj[checkProp];
    } else {
     return "Not Found";
  // Only change code above this line

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

I’d start by getting rid of this. You want to test the obj being passed into the function. When you add this you are overriding that obj with your own.


It works. Now I have to figure out why adding this will override the obj . Thanks so much!

I’d google “variable shadowing”.

1 Like

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