Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

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


console.log(checkObj("sleigh"));


  

Your browser information:

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

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

Hello and welcome to the forum!

I think you’ve misunderstood the purpose of this challenge a little.

You are supposed to modify the existing function, so that ANY object which is passed as the first function parameter (obj) can be checked for ANY property which is passed as the second function parameter (checkProp).

What you should do, with the checkObj function, is write a simple logical statement, using the .hasOwnProperty() method, to check if checkProp is a property of obj.

If that statement is true then you return the value which obj has for the checkProp property. If it is false, you return ‘Not Found’.

You should not be inserting any object directly inside your function.

For instance, if I called the function as:
checkObj({ top: 'hat', bottom: 'pants' }, 'top')

The function should return ‘hat’ because obj has the property top and the value of top in the object is ‘hat’.

However, if I call the function as:
checkObj({ top: 'hat', bottom: 'pants' }, 'middle')

The function should return ‘Not Found’ because middle is not a property of obj.

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