Basic JavaScript - Testing Objects for Properties

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

So I already managed to complete this challenge by looking up some advice on some older forum posts, but I still have no idea why this code works nor could I find an understandable explanation.

Basically the example shown in the test was how to use .hasOwnProperty to show whether it was true or false if a given object had a property name or not.

Example Given:
(const myObj = {
top: “hat”,
bottom: “pants”
};

myObj.hasOwnProperty(“top”);
myObj.hasOwnProperty(“middle”);
The first hasOwnProperty returns true, while the second returns false.)

The test itself on the other hand involved using .hasOwnProperty on an argument inside of a function to test, as best as I can tell, for the presence of a second argument contained within the function?

So basically what I don’t understand is-

1: How does using .hasOwnProperty on a function, or possibly the argument inside of a function once again I’m not sure, relate to using it to search for a parameter inside of an object?

2: Why am I using obj.hasOwnProperty(checkProp), obj the argument, to check whether or not it is true the function checkObj contains an argument called checkProp. Shouldn’t I be using checkObj.hasOwnProperty(checkProp), since it is the function that contains the argument? And how does using .hasOwnProperty on an argument show that there is another specific argument contained within the same function?

If someone could please explain this to me that would be really nice.

Your code so far

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
}

Your browser information:

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

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

We have blurred this solution (with [spoiler][/spoiler] tags) so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

1: How does using .hasOwnProperty on a function, or possibly the argument inside of a function once again I’m not sure, relate to using it to search for a parameter inside of an object?

I don’t understand the question as written. We are not “using .hasOwnProperty on a function”.

obj is an object. Like all objects, it inherits certain properties and methods. One of those methods is “hasOwnProperty” It will tell you if that object has a property of that name.

2: Why am I using obj.hasOwnProperty(checkProp), obj the argument, to check whether or not it is true the function checkObj contains an argument called checkProp. Shouldn’t I be using checkObj.hasOwnProperty(checkProp), since it is the function that contains the argument? And how does using .hasOwnProperty on an argument show that there is another specific argument contained within the same function?

Again, I’m not sure I understand:

Why am I using obj.hasOwnProperty(checkProp), obj the argument, to check whether or not it is true the function checkObj contains an argument called checkProp.

You are not checking “whether or not it is true the function checkObj contains an argument called checkProp”. You have a function called “checkObj”, you are saying, “I will send you an object called ‘obj’ and a string called ‘checkProp’ - see if there is a property in that object whose key matches that string.”

I think it is a little confusing variable naming - in “checkObj”, “check” is a verb", but in “checkProp”, it is being used as a noun. I think it would have been clearer if the string were called “propName” or something.

Shouldn’t I be using checkObj.hasOwnProperty(checkProp), since it is the function that contains the argument?

No. “checkObj” has the argument of “checkProp”, but we know that. We care if the object “obj” has that key, not the same thing. We don’t need to refer to “checkObj” because it gives us the parameters and stores it in those variables.

And how does using .hasOwnProperty on an argument show that there is another specific argument contained within the same function?

It doesn’t know care that it is an argument and doesn’t know or care that it is in a function. All it knows is that this function is getting called - obj.hasOwnProperty. That is a function that is part of obj, a method. It is saying, “Hey, this object here, to which I am attached, does it have a property whose name matches the string I just got sent?”

So arguments also count as objects? The same as objects containing key value pair lists like the past few lessons worked on?

Okay so if I’m reading this right arguments, like for example obj, are once again to reiterate also count as objects, and the goal of the test was check the argument/object obj for if it contained a string called “checkProp”.

So then the fact that the function containing the obj argument/object, checkObj, also contained a second argument also called checkProp was just a coincidence and not relevant to using .hasOwnProperty to test if obj contained a string called “checkObj”.

Objects can be arguments to a function but not all arguments are objects.

An argument can be any datatype (number, string, object, array, date, function). An argument is what gets passed to a function.

No. The goal of the test was to check if the object named obj contained the value contained in the function’s argument. checkProp is the function parameter (a variable local to the function that is the same as the argument passed to the function.

Function arguments are what get passed to a function when a function is called. Function parameters are the local placeholder variables that represent the same arguments that get passed to a function. Example:

function myFunc(myFuncParameter) {
  console.log(myFuncParameter);
}

The above function named myFunc has one parameter myFuncParameter. You can see that inside the function, it can be referenced like a variable because it is a variable.

myFunc('some data');

The above line of code makes a call to myFunc and passes the string 'some data' as the argument to the function.

When the code executes, the string 'some data' gets displayed in the console, because the parameter myFuncParameter contains the value 'some data'.

Okay, but if I’m using .hasOwnProperty to check whether it’s true or false that something contains a property called checkProp, and not a string that is just also coincidently called “checkProp”, why does the property obj contain the property checkProp?

Do all properties listed as arguments in a function just automatically contain each other?

In this particular challenge, the function named checkObj has two function parameters, so it will accept two arguments passed to it when the function is called. The first argument passed to it will be represented by the parameter obj and the second argument passed to will be represented by the parameter checkProp. In all the test cases, the data type of checkProp will be a string. You can validate that by looking at the second argument of each test case function call.

You can only use the hasOwnProperty method on an object. Inside the function, that happens to be represented by obj and can a have a different value for each function call. There are no properties named checkProp. It is a variable named that holds a string value that was passed into the function. You are to use this variable as the argument to the hasOwnProperty method to get a return value of true or false.

Methods are functions, so they can have arguments (values passed to them). hasOwnProperty accepts an argument. If it is not a string, it will get coerced into a string.

Wait hold on, you just said that checkObj has two parameters, obj and checkProp, but that there is no property called checkProp?

So is the checkProp I’m checking to see if it’s contained in the parameter obj not the same as the checkProp listed in checkObj as a parmeter?

Ok I’m beginning to think there might be a miscommunication here. This is what I understand so far.

If I type the name of an object, the method .hasOwnProperty, and then the name of a property then the method .hasOwnProperty will return either true or false depending on whether the listed object contains the listed parameter. For example-

obj.hasOwnProperty(checkProp)

checks to see if the object obj, which in this case is also a parameter for the function checkObj, contains a parameter called checkProp. If it does it returns true, if it does not it returns false.

What I don’t understand is why obj and checkProp both being parameters listed inside of the function checkObj means obj contains checkProp.

Is the existence of the second parameter called checkProp contained within the function checkObj relevant to using .hasOwnProperty on obj to test for a peice of data called checkProp?

No I did not say that. I said checkObj has two parameters.

Objects have properties. I said none of the objects passed as arguments to the function have a property named checkProp. checkProp is a function parameter that contains the value pass as the second argument of the function.

But you did.

Unless, is a function parameter different from a normal parameter? Or are you talking about the parameters written inside the function-

function checkObj(obj, checkProp)

or the theoretical arguments that can be sent to a paremter?

Either way that was not my question.

Is the checkProp I’m testing to see whether or not it’s contained in obj the same as the checkProp listed directly to the right of obj in the function? Once again specifically-

function checkObj(obj, checkProp) <-This one right here.

It doesn’t. The logic in the function body for checkObj needs to check if the object given by the function parameter obj has a property with the name given by the function parameter checkProp.


I think you are making this more complicated in your mind than it really is.

checkObj() is a function. checkObj() accepts two arguments, obj and checkProp. obj is literally any object. checkProp is literally any string. You need to write logic that checks if the string stored inside of checkProp is actually a property name for the object stored in obj.

You should use these function parameters just like you would any other variable:

const myObj = {
  top: “hat”,
  bottom: “pants”
};

const myProp = "top";
console.log(myObj.hasOwnProperty(myProp));

Both obj and checkProp are parameters. They are just variables holding the values of the arguments passed to the function. When you reference checkProp in the function, it is the same as referencing any other variable. Variables represent values. In this case checkProp represents the various strings that will be passed to the function. checkProp is a string but its value is what ever gets passed as a second argument of the function. It does not contain a value of "checkProp". It just represents a string that might be passed such as "top" or "bottom".

No, checkObj is the function and checkProp is one of the two parameters listed in it. I didn’t write those parts those were part of the original given code I was instructed not to alter.

This was the initial code I was given.

function checkObj(obj, checkProp) {
// Only change code below this line
return “Change Me!”;
// Only change code above this line
}

Also, do not confused word “property” with “parameter”. Those are two different things. Objects have properties. Functions have parameters.

Ok, I made a typo. Fixed:

I think you are making this more complicated in your mind than it really is.

checkObj() is a function. checkObj() accepts two arguments, obj and checkProp. obj is literally any object. checkProp is literally any string. You need to write logic that checks if the string stored inside of checkProp is actually a property name for the object stored in obj.

You should use these function parameters just like you would any other variable.

1 Like

So the second parameter being called checkProp is irrelevant to checking for a checkProp in obj.

For example it could have been written as -

function checkObj(obj, somethingOrOther) {
  // Only change code below this line
  if (obj.hasOwnProperty(checkProp)) {
    return obj[checkProp];
  } else {
    return "Not Found";
  }
  // Only change code above this line
}

-and it would still have worked?

No, that would not have worked at all. checkProp is not defined anywhere. You cannot use undefined variables.

checkProp is the same checkprop that you are trying to check if the object obj has a property of the same name that checkProp contains.

No.

You would reference somethingOrOther as the argument to the hasOwnPropertymethod. The argument names can be anything but to make the code more readable, we typically try to give them a name that represents what the value is.

Based on your replies to our questions, I strongly recommend reviewing the previous challenges before continuing and make sure you understand the concepts/syntax in them.

Ok that’s the part that’s confusing me, and what I’ve been trying to ask. Not the things you linked to which I already did, doublecheck, and get.

If obj.hasOwnProperty(checkProp) is checking to see if obj contains a parameter called checkProp, at what point in the code is checkProp placed inside of obj?

As best as I can tell checkProp is only listed in the code as a paremeter for the function checkObj.

That has nothing to do with your function. The obj is defined outside of your function and passed in when the function is called.

It is not at all a property of the function. A property and a function parameter are different things.


I don’t think you understand function calls or parameters.

A function parameter is a variable that is defined as part of function as an input. The value of the parameter is determined by the arguments when the function is called