Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:
how do I return “not found”, the 2nd time I pass the object?

  **Your code so far**
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
}
  **Your browser information:**

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

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

You are overwriting the function argument obj. You must not do this.

so, I changed to myObj ={ …}
but still getting the same message…

checkObj({pet: "kitten", bed: "sleigh"}, "gift")

should return the string

Not Found

.

The tests pass in various objects to the function which are accessed by referencing the obj parameter, so there is no need to create any additional objects here.

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

Do not create myObj. You must use obj. You must not create any new objects inside of your function.

Hi, if I dont create, how do I list myObject? Thanks for your help!

You don’t. You create no objects! The object is passed into the function as an argument when the function is called.

Finally got what you are saying, lol, thank you!

1 Like