Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:
Describe your issue in detail here.
my question is incomplete…the check box has items that don’t exist in my question

Your code so far

function checkObj(obj, checkProp) {
  
checkObj({
  gift: "pony",
  pet: "kitten",
  bed: "sleigh",
  city: "Seattle"
})
// Only change code below this line
  return checkObj.hasOwnProperty(obj);
  // Only change code above this line}
}
function checkForProperty(object, property) {
  return object.hasOwnProperty(property);
}

checkForProperty({ top: 'hat', bottom: 'pants' }, 'top'); // true
checkForProperty({ top: 'hat', bottom: 'pants' }, 'middle'); // false

Your browser information:

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

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

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).

You have created a second function for some reason?

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’.

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.

2 Likes

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