I am stuck on Testing Objects for Properties

So I’m stuck on this lesson. I’ve worked out via forum posts I’m not supposed to put anything about kittens and gifts. I think I’m not 100% sure as this lesson is confusing me a lot. I still don’t understand what to do after reading those forum posts though.

But if there’s no objects to test, how does this object testing function work?

Can someone explain this to me please.

  **Your code so far**
function checkObj(obj, checkProp) {
// Only change code below this line
obj.checkObj("");
// Only change code above this line
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15

Challenge: Testing Objects for Properties

Link to the challenge:

I’ve tried this but it doesn’t work. No idea if I’m on the right track or not.
I know forum posts seemed to suggest not to add the stuff about kittens and pony’s but I have no idea what else I could possibly be expected to do.

function checkObj(obj, checkProp) {
// Only change code below this line
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"});
if
(checkProp.hasOwnProperty("pony")){
return "gift";
}
else if (checkProp.hasOwnProperty("kitten")){
return "pet";
}
else if (checkProp.hasOwnProperty("sleigh")){
return "bed";
}
else {
return "Change Me!";
}
// Only change code above this line
}

First thing you need to understand is the function that’s already set has two arguments passed into it and you basically need to work with those arguments inside the function. For easy understanding you can copy and paste one of the tests that are given (outside the function). You’ll see that one of them is an object and the other is just a string. So the first argument of the function was an object and second was a property name that you needed to check if it’s actually inside the object or not.

function checkObj(obj, checkProp) {
 // Only change code below this line
    return "Not Found";
  }
  // Only change code above this line

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")

Now you just need to use the function argument names - obj and checkProp and treat them as and object and a property name. Notice the explanation on the left hand side, you can see that to check if an object has a property or not it uses a method called “hasOwnProperty” and returns true or false if the property name is inside the object or not. In the example I’ve shared above, the property name would be gift, pet and bed.

checkObj is function you know what functions do ? a typical function takes input and returns some output, so here checkObj ttakes an obj and checkProp arguments as input, obj is an javascript object which is already populated with properties and checkProp contains name of a property to check if it is in the object hence hasOwnProperty to check it, if it is there then hasOwnProperty will return true so you have to use it in if statement and return the value of that property you don’t have to go through the obj to check each property

Hi, I tired to do something like this but it doesn’t work. I know what functions are but I don’t understand how to make this work still.

function checkObj(obj, checkProp) {
 // Only change code below this line

if (checkProp.hasOwnProperty;){
  return "true";
}
else {
 return "false";
}
 // Only change code above this line
}

How do I treat obj and checkProp as an object and a property name?

I don’t understand how to do that.

Let me phrase it a bit differently. The arguments that are passed into the function are an object - obj and property name - checkProp. When you call the function with both an object and a property name, it then checks if the property is part of the object or not and returns a true or false value. So the argument obj would be for the object that is passed into the function and checkProp would be for the string value (which is a property name) that is passed into the function. You have to use the argument variables obj and checkProp, to write the code inside the function.

I’ve got this now.

I’m not sure what I need to put for the ‘if’ part. I know I need to find out if the property name is inside of the object but I still don’t know how.

function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.checkProp){

}
else {
return "Not Found";
}
// Only change code above this line
}

When you open the FCC challenge or test, the explanation provided on the left hand side has an example. You can see that they use a JavaScript method called “hasOwnProperty”, which checks if a property name is inside the object or not. And that property name is passed into the method via a string value.

myObj.hasOwnProperty("top");
//you can also see this in the explanation side that I mentioned.

You can put that in your if statement and return true.
You can read about hasOwnProperty method on the MDN website. It will help you understand.

Hi, I did look but I when I tried to use the code from the example as a template it didn’t work. I understand the example and how it worked but not how to apply it to my own code.

I’ve tried many things and am getting nowhere. I feel like giving up because nothing I do works.

I have this now. I know it is wrong but I just do not understand this lesson or why nothing I do works.

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

Inside the if statement bracket, you just need to use the variables for your own function which is obj - for the object that is passed into the function and checkProp - for the string value (which is a property name) that is passed into the function. And use the “hasOwnProperty” method on your object (which is obj) to check the string value (which is checkProp).

Hi. I have this now. Which doesn’t work still. I really do not understand what I am supposed to do.

I swear I have done the rest of the curriculum and am not skipping lessons or anything and I’m looking up online trying to get this to work. I just don’t know why I can’t do this. :worried:

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

the syntax for using this method is first writing the variable name for your object (because you’re checking whether or not your object has a property, so you would use this method on the object) which in your case is - obj. And then writing the name of the method, which is “hasOwnProperty” then brackets and inside the brackets would be the property name that you want to check, which in your case is checkProp. Now you can put whatever it is you want to return inside the curly braces.
One thing to point out, the if statement checks for the condition true, unless you explicitly say that you want it to check for the false. So when you pass the hasOwnProperty method like I did in the code above it is already checking for the true returning value for that method. I don’t have write if(obj.hasOwnProperty(checkProp) == true){ }. You can write it like this, it’s not wrong. It’s the same thing as I did in the example above, except here I’m explicitly writing it.
Hope you’re able to solve the problem now.

No I can’t solve it. I have tried the below code and it still doesn’t work.

‘the syntax for using this method is first writing the variable name for your object’

So I need write the variable name for the object? Where do I write this?

I tried using const but got the message ‘ SyntaxError: unknown: Identifier ‘obj’ has already been declared. ’

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

You need the if(obj.hasOwnProperty(checkProp)){} to check if the property is inside the object or not. The challenge is to return the property’s value if it’s present and to return Not Found if it’s not.

I have this now that passes some of the coding requirements, but not all.
I think I get now the examples and how they apply. obj is ‘gift’, ‘pet’, ‘bed’ the object. checkProp is the values/strings assigned to those things. I need to return the values.

I’m getting there I think. Still not quite got it though. It returns ‘Not Found’ when it should at least.

I don’t know what I need to put for the ‘if’ return still to make it work.

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

I figured it out!!!

I read back though what you said a few times and got it.

1 Like

It’s great that you figured it out. But I want to make one thing clear just in case because you said this - obj is ‘gift’, ‘pet’, ‘bed’ the object. checkProp is the values/strings assigned to those things - earlier.
in this example (taken from the same challenge) -

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")
{gift: "pony", pet: "kitten", bed: "sleigh"}  //Is the object 
"gift"  // is the property name 
"pony" // is  the property value for the gift property name
/*{gift: "pony", pet: "kitten", bed: "sleigh"}  was the obj and 
 "gift" - the string value passed into the function - was checkProp
obj.hasOwnProperty(checkProp) was (in this case) checking if the gift
property name was inside the obj (or object) or not.
And if it was then you would return the property value for that property name
which was "pony".
*/

I know that you’ve now solved this but just to make things clear the challenge required the property value to be returned if the property name was present inside the given object that was passed into the function.
Sorry I couldn’t reply to you earlier but I just wanted you to not learn anything that is incorrect.