Testing Objects for Properties: About to put my fist through my monitor. What am I doing wrong?

Tell us what’s happening:
I think my frustration levels are starting to boil over here… so I might just stop right now and go to sleep before I flip my desk. But I can’t figure out how to solve this assignment. I have been staring at it for hours, and I know it is probably super duper easy to figure out.

I assume I use the object properties from the test lines it is supposed to run? The instructions don’t explicitly tell me which ones to use, so I just used gift, pet and bed. I have no idea if that is correct. Can someone please tell me if I am even on the right track, or should I just erase it all and start over?
Thanks.

  **Your code so far**

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

const obj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
}

if(obj.hasOwnProperty(checkProp)) {;
  return checkProp;
} else {
  return "Not Found";
} 

checkObj(obj, checkProp);
// Only change code above this line
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0

Challenge: Testing Objects for Properties

Link to the challenge:

Why did you redefine obj? You should use the function argument without replacing it like this.

Hi there… please forgive my thick skull. Can you explain what you just said? (I have been trying to backtrack assignments to make sense of what any of this means… I am just super lost right now.)

This says that an obj and checkProp are passed into the function.

But here

You are explicitly ignoring the obj passed into the function and replacing it with something completely different.


checkObj({city: "Seattle"}, "district")

Here the function should be using the object passed in, but this object is overwritten in your code. Let the function call define the inputs and don’t overwrite them.

Oh man, I am so embarrassed to say that I still don’t know what is going on. I was doing so well up to this point. Urghh. I really don’t want to look at the answer.

Ummm… soo… all the tests that run; isn’t there supposed to be an object with properties in it? The instructions didn’t tell me what object to build so I built it according to the tests that it will run. Again, I am very sorry.

Do not build any object. Your function should create zero objects.

Right here obj already exists when the function is called. You don’t need to build it. Just use obj.


This says, "call the checkObj() function with obj = {city: "Seattle"} and checkProp = 'district"

OK thanks. I can only say, at this point I am still lost. I think I will try and sleep, then come back to this tomorrow and start from scratch. This whole course is really making me question my intelligence. LOL. But thanks for trying with me. I might have a clearer head tomorrow.

Learning this stuff is hard. It’s all good. You are 90% there. You just need to delete 5 lines.

1 Like

Ok. Another hour looking at this today after some breakfast and a coffee, and maybe I am closer, but I still can’t get the tests to pass. I checked for spelling mistakes. I think have all my curly brackets correct etc. I checked back to previous assignments but could not find any that are similar to what I am doing here…

I am telling it to check a property (obj) for a value (checkProp) and if true, return checkProp. If false, then print a string “Not found”. Unless I am misunderstanding what the assignment is asking (wouldn’t be the first time), then I can’t see what I am doing wrong. I am just flabbergasted that this is so damn complicated for me… I know it’s probably really easy. :unamused:

One thing that I just can’t, for the life of me, wrap my head around, is how I am supposed to check for an object and a property, when there is no object or properties in the code! Prior to this assignment, there were objects with a list of properties in it and we can search through those objects to find properties. Here, there is nothing… just a function with no object in it.

function checkObj(obj, checkProp) {

  // Only change code below this line

  if (obj.hasOwnProperty(checkProp)) {

    return checkProp;

  } else {

    return "Not Found";

  }

 checkObj(obj, checkProp);

  // Only change code above this line

}

You are close…

Once you tweak those, lets circle back to ‘where’ the objects are.

Thanks again for the reply. You are too kind and much too patient with my dumb ass. lol :sweat_smile:

The way I understand (which I’m sure is wrong), is that obj and checkProp are brought into the function and that there should be some code in the function that uses obj and checkProp. In ALL the past examples, inside the function, you can see an object

I know I am dense, but I think this might be a bit of a learning gap in FCC’s curriculum. I feel like we are skipping over something kind of important here.

Anyway… I have thought of what you were asking here.

checkObj(obj, checkProp) is taking arguments (I think is the right term?.. they are like variables right?) into the function. So that code inside the function can use them? Shouldn’t there be some sort of “list (properies?)” along with an object that will then get those arguments? I am having problems putting what I am thinking into words here. My terminology is poor.

you can call the function yourself at the bottom and use that to help you see the values.

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")
post that at the bottom and you should see them in your function to help you see stuff happening.

but like jeremy said, you are very close to what you need to solve this. is the test asking for check prop, or the value?

YES, what I wish I had was some more feedback about what my code is doing while I am doing it. So that is a great idea. Thanks.

I am going to keep working at this.

EDIT: I didn’t see anything happening with that line of code… do I need to console.log something? perhaps console.log(checkObj)?

make sure it is outside your function, and yes console.log(checkProp) inside the function for the value of checkProp being passed to the function.

you can also console.log( checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")) to see the return value of your function.

OK, thank you. THIS was absolute key. :partying_face: Without seeing what is happening in my code I have no idea if I am even on the right track. So that was key advice. I wouldn’t even have any clue how to properly console.log this code, so that was a big help.

I got hung up on the syntax quite a few times trying to get this to work:

  if (obj.hasOwnProperty(checkProp)) {
    return (obj[checkProp]);    // <---- this part here with the proper brackets
  } else {
    return "Not Found";
  }

So the assignment gave me a pass on this one. I feel relieved, but not exactly triumphant. Good thing I already have an IT job because I will be 90 years old by the time I become a competent coder at this rate! :rofl:

Thanks to you both for helping. I could imagine it was quite painful dealing with me. Much appreciated.

This is what I was trying to get at here. The obj and checkProp are created outside of your function and passed into your function as arguments when called.

Yes, I see that now, but I thought all that code to be executed had to be within the function. Including the object. I didn’t know you could pull objects from outside a function into a function. And I think I thought this because prior to this point, there were no exercises practising how to bring objects into a function. All the objects were right there in plain sight.

I tend to enjoy FCC… most of the time. It is a pretty decent learning resource and I am glad to use it, and will do my best to continue to use it… HOWEVER, from an absolute beginners perspective, I think it needs to be more project-oriented. I think if I could see and understand how code OUTSIDE of the functions interact with functions, then this would be a whole lot clear. Its like trying to view the world through a paper towel tube. It is quite narrowed in… if that makes sense.

I am not complaining, but building projects that make real-world use of these lessons would be absolutely stellar. I heard that FCC might be overhauling the JS curriculum in this manner. If that is the case, I will be very excited to try it out.

Thanks for the help!

We’re working on a big overhaul to shift more towards projects.

That said, there is a huge difference between following along on project instructions and making your own. Putting multiple things together that you have previously seen is just hard and its normal to struggle. Programming is hard.

1 Like

I might start supplementing FCC with Odin. I think the two would compliment each other well. Odin’s HTML and CSS projects were right on the money. Building something functional was quite satisfying. I moved to FCC to get a sense of what JS is all about. I might revisit Odin for JS and sort of run the two side by side to get the best of both worlds. FCC for narrowing in on very specific concepts, and Odin for putting those concepts into real world projects.

Multiple resources is a good thing.