Basic JavaScript - Testing Objects for Properties

I am Confused😫

I don’t understand the question that was given about the .hasOwnProperty( ) method. I have tried several things but i keep getting an error and it didn’t even tick anyone correct… I need assistance🙏

Your code so far

function checkObj(obj, checkProp) {
  // Only change code below this line
  if (checkProp){
    
  }
  return checkProp.hasOwnProperty({"name" : "Pony"});
  // 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/118.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Testing Objects for Properties

The hasOwnProperty can only be called on an object. You are calling it on the checkProp string passed into the function.

“To check if a property on a given object exists or not, you can use the .hasOwnProperty() method.”

This means if you have an object named obj and you want to check if it has the property “name” then you would use the hasOwnProperty method on obj and pass in the string “name” and the method would return either true or false. Or, if the property name was stored in a variable called checkProp then you would pass that variable into hasOwnProperty.

You basically need to do what I described above and then based on the value hasOwnProperty returns your function will either return the property’s value or the string “Not Found”.

The .hasOwnProperty() method is used when want to check whether an object has a certain property.

The syntax looks like this:

object.hasOwnProperty(property)

This method only work on objects - an array that starts and ends with curly brackets {}.

Where object is the name of the object.
property is the name of the property you are checking for, which goes inside the curved brackets.

Just adding to @bbsmooth.

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