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

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?

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?

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

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
}

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.

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

The property part was a typo, but where is obj defined outside of the function? It’s literally inside the function.

function checkObj(obj, checkProp)

The parameter is inside of the function. The value of the parameter is set when the function is called. If you never call the function yourself, you never see the value of the parameters get set.

But just because you don’t see it doesn’t me that isn’t how it works

Okay, this time I’m going to try going through it line by line.

function checkObj(obj, checkProp) {

declares the function called checkObj to exist and posses the parameters obj and checkProp.

if (obj.hasOwnProperty(checkProp)) {
return obj[checkProp];

An if statement that checks to see if it is true or false if obj contains checkProp. If true it then it returns the value assigned to checkProp.

} else {
return “Not Found”;
}
}

If it is false that obj contains checkProp it instead returns the string “Not Found”.

Where is checkProp placed inside of obj, or is it not specifically placed inside of it here and is that unnecessary for completing the test?

Please try pointing to an actual line of code.

Never.

Modifying the obj is not part of the instructions

Okay then, just to confirm. I’m not asking about theoretically assigning a specific argument. I’m asking if there is anything in the code as it is written and only as it is written that will give a true result from obj.hasOwnProperty(checkProp).

Once again, not could I input an argument that would result in it being true, but is there already or not something in the code I wrote that would do so?

I think you are still missing the point here. The return value of that function call right there depends upon the arguments to the checkObj function call.

The point of this exercise is not to force that to be true.