Need help with Testing Objects for Properties

Tell us what’s happening:
So I was wondering what exactly is wrong with this: if(obj.hasOwnProperty(“checkProp”)) line of my code. The solution says it should be: if(obj.hasOwnProperty(checkProp)) , the only difference being the quotation marks around checkProp.

If I use quotation marks, I fail the challenge, however the examples shown with this challenge use quotation marks!

Also, is there are an obligation to only use bracket notation to access properties of objects in this kind of scenario? I ask this because whenever I use dot notation instead of bracket notation, the challenge fails.

I am very confused. I spent two days on this challenge trying to solve this challenge without taking any help from the solutions (finally gave up today) but even after looking at the solution, I cannot fully grasp where I went wrong which is driving me nuts…

Any kind of help will be much appreciated by my brain which is begging me for a break!

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";
}

return "Change Me!";
// Only change code above this line
}

Your browser information:

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

Challenge: Testing Objects for Properties

Link to the challenge:

try:

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

The line above is checking if obj has a property actually named “checkProp”. There are no properties with that name. I suggest reviewing the following 3 challenges:

Welcome, Sayeedur.

This is not just specific for .hasOwnProperty, so I am going to give an example with indices.

const myObj = {
  key1: "someValue",
  key2: [1, 2, 3]
}
const someVariable = "key2";

console.log(myObj.key1);
console.log(myObj[key1]);
console.log(myObj["key1"]);
console.log(myObj[someVariable]);
console.log(myObj.someVariable);
console.log(myObj["someVariable"]);

Go and give these a shot in the browser console to see what happens.

Thanks a lot man! Cleared up a lot of things for me.