Testing Objects for Properties assistance request

Challenge Link -https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties

checkObj({pet: "kitten", bed: "sleigh"}, "gift")should return the string Not Found

Is the error I am encountering

function checkObj(obj, checkProp) {
  // Only change code below this line
   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
}

Is my code. I tried to check for issues, but cannot muddle through this one.

Why are you redefining obj inside of your function?

It’s already being passed in as an argument to your function

When I try to run it without, then it refuses to pass any of it. Until I go back now and remove it and it works. This is getting kind of frustrating, when I originally tried it as just a if statement it bounced back, can you explain why that would me? I have had that happen a few times where something does not work, then it does.

You must have changed more than than just adding and removing the obj. Small changes have big impacts in code.

I am glad I fixxed it without thinking about it, but wish I knew what I fixxed.

Unfortunately, it’s impossible to know for sure unless you kept track of your changes.

Works for me with obj declared outside the function, which makes sense given it’s a param. Ran this in VSCode with node with no problemo:

function checkObj(obj, checkProp) {
    // Only change code below this line 

    if (obj.hasOwnProperty(checkProp)) {
        return obj[checkProp];
    } else 
    {
        return "Not Found";
    }

    // Only change code above this line
}

obj = {
    gift: "pony",
    pet: "kitten",
    bed: "sleigh",
    city: "Seattle",
};

let result = checkObj(obj, "wombat");

console.log(result);

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