Basic JavaScript - Testing Objects for Properties

Don’t understand the explanations on previous posts so wanting to get some help here please.

This is what I have so far, but I honestly have no idea where to start ):

function checkObj(obj, checkProp) {
// Only change code below this line
if obj.hasOwnProperty(checkProp)) {
  return 
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/103.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

What do you need to return when the property is found?

Need to return obj? (need 20 characters so typing this lol)

No, you should not return the entire object.

If the property is found, return that property’s value.

ok so then true maybe?

You could return the Boolean literal true, but that is not what the instructions say. The instructions say return that property’s value. The value associated with the property will not be the Boolean literal true.

Would it be checkProp then?

Modify the function checkObj to test if an object passed to the function (obj) contains a specific property (checkProp)

checkProp is the property. You want the value associated with the property.

So I was right, it IS checkProp?

No.

The value is not given yet. It will be given by the input in the test, correct?

No.

You have two inputs:

  1. The object

  2. The name of the property

You have two possible outputs

  1. The value associated with the property inside of the object

  2. The string “Not Found”

Alright, I’m going to take a long break and come back because I am lost.

Review Accessing Object Properties with Variables to see if this gives you an idea of how to get the value of the object’s property.

Already I went through that excersize you all sent.

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

I just don’t see a property here to put in []

What is checkProp?

I was getting kind of frustrated so I found the answer online and would like a step-by-step explanation as to why this is the answer. I guess the way the problem was worded, I didn’t understand that any of that meant to use obj again ):

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

How about you try to explain what you think this code is doing instead? We can correct any misunderstandings. Go line by line.

That’s the thing —now that I have the answer I can kind of read it, but I’m not in a classroom to stop the teacher and have them explain how they knew to get the answer they got.