Help me to find answer

The test:

Testing Objects for Properties

Sometimes it is useful to check if the property of a given object exists or not. We can use the .hasOwnProperty(propname) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.

Example

var myObj = {
  top: "hat",
  bottom: "pants"
};
myObj.hasOwnProperty("top");
myObj.hasOwnProperty("middle");

The first hasOwnProperty returns true , while the second returns false .

Modify the function checkObj to test if an object passed to the function ( obj ) contains a specific property ( checkProp ). If the property is found, return that property’s value. If not, return "Not Found" .

My answer:


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
}
  **Can someone help me?:**

Challenge: Testing Objects for Properties

Link to the challenge:

Hi @d-edward !
Welcome to the forum!

A couple of issues.

No.1
You should not create an object here. You can can delete this.

Reason being, is because you want to write a function that works with any object not just one that you hardcoded yourself.

No.2:
You need to use checkProp but not in quotes.
So this wrong

Once you remove the quotes and delete that object then the test should pass.

2 Likes

wahh thanks, i can learn from this mistake :smiley:

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