Testing Objects for Properties_

I am unable to solve this logic
checkObj({pet: “kitten”, bed: “sleigh”}, “gift”) should return the string Not Found

I am using the following code:

function checkObj(obj, checkProp) {
 // Only change code below this line
// Setup
var myObj = {
 gift: "pony",
 pet: "kitten",
 bed: "sleigh"
};

myObj.city = "Seattle";
 // Your Code Here
let result = "";
 if (myObj.hasOwnProperty(checkProp)){
   result = myObj[checkProp];
 } 
 else{
   result = "Not Found";
 }
 return result;

// Test your code by modifying these values

 // Only change code above this line
}console.log(checkObj("gift"));

Challenge: Testing Objects for Properties

Link to the challenge:

you are testing this object instead of the one passed in the function:

what amendments I need to do in the following code? Please tell me what I am actually missing?

Why you are creating an object even though the test dont want it.

Delete that object ,you are getting the object as argument.

function checkObj(obj, checkProp)

Check the condition on that object

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

you need to have a reusable function, that means you should have a function able to test any object, not just the one you created yourself - so delete the one you created yourself and test the passed in object

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

}

var myObj = {

  top: "hat",

  bottom: "pants"

};

console.log(checkObj(myObj, 'top'))```

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