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
  checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")
  checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")
  checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")
  checkObj({city: "Seattle"}, "city") 
  checkObj({city: "Seattle"}, "district")
  checkObj({pet: "kitten", bed: "sleigh"}, "gift")
  return "pony, kitten, Not Found, Seattle, Not Found, 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/113.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

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

You appear to be trying to call the function within itself, explicitly checking only certain values and then returning a string of words.

What you should do 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.

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