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?

You will need to post your entire code for us to guide you better.

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.

2 Likes

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