Testing Objects for Properties Challenge Difficulties

Good evening.

I am struggling with this challenge and no matter what I attempt, it keeps returning “Not Found”.

Here is my code below:

// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  // Your Code Here
  
  if(myObj.hasOwnProperty() === true) {
    return checkProp;
  }
  
   else return "Not Found";
}

// Test your code by modifying these values
checkObj("pet");

Any help and feedback is greatly appreciated.

Thank you.

1 Like

In according to hasOwnProperty :

if (myObj.hasOwnProperty (checkProp) === true ) {
  return checkProp;
}
var foo = {answer: 42};

if (foo.hasOwnProperty('answer')) {
  alert("Wowzers!");
}

Spotted it?
hasOwnProperty needs the name of the property to check if the object has it or not. Also, you don’t need to compare the result with true, because if already checks if the result is truthy.

1 Like

Why do you use “alert” in your code?

I have tried to change it using the guideline you have provided and now it returns checkProp and no longer returns “Not Found”.

var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  // Your Code Here\\
  
  
  if (myObj.hasOwnProperty('gift')) {
    return checkProp;
  }
  
   else return "Not Found";
}
// Test your code by modifying these values
checkObj("house");

It returns “house” in this case.

What am I missing here? This has been my most frustrating challenge yet :disappointed_relieved:

1 Like

I have tried altering the code using this and it still returns “Not Found”

// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  // Your Code Here\\
  
  
  if (myObj.hasOwnProperty(checkProp) === true) {
    return checkProp;
  }
  
   else return "Not Found";
}

// Test your code by modifying these values
checkObj("house");

Yep, ‘house’ doesn’t exists in myObj.

Your function was called from checkObj (“house”); in the last line, if you change it with checkObj (“gift”); it returns “gift”.

Yes. But if I input “gift”, it should return “pony” and it is not. That is where my frustration lies. My return is not producing what I want to to return and I feel that I am missing a vital step but cannot pinpoint that it is at this moment.

I am honestly not seeing it.

1 Like

Oh ok :slight_smile:

if (myObj.hasOwnProperty(checkProp) === true) {
    return myObj[checkProp];
}

“pony” is the value of “gift” property in “myObj” object => myObj[‘gift’] = “pony”

2 Likes

Thank you. Worked like a charm.

I now get where I was getting it wrong. It is important to remember to use the write code.

A lot has been learned in the past few days and it is a lot to take in. I guess practice will make perfect.

Thank you so much for all your help NeckersBOX!!! :relieved: :dancers:

:wink: you’re welcome! Good coding !!

FYI, you don’t need that part in the condition.

(myObj.hasOwnproperty(checkProp)) will be True if the object has that property and False is if it doesn’t.

Try it out.

1 Like

Good morning the-thief.

Yes you are correct in saying that. I have removed the

in the code and it returns the correct output.

Thank you

Hey guys. Thank you for your contribution on that topic. It helped me spot what I am doing wrong, but now the question is: Why the dot notation to access objet properties will not work here? Here is my code:

// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  // Your Code Here
  if (myObj.hasOwnProperty(checkProp)) {
    return myObj.checkProp;
  }
  
  return "Not Found";
}

// Test your code by modifying these values
checkObj("gift");

If I change it to bracket notation it will work, i mean this bit:

if (myObj.hasOwnProperty(checkProp)) {
    return myObj[checkProp];
  }
1 Like

because when you use obj.property is the same thing of obj['property']

1 Like

you mean i can use it only if know the exact name of the property? so for function parameters will never work?

Yes, if you use . you should know the property name requested, else you need to use [] to call the name of property inside a variable.

var obj = { cat: "meow", prop: "prrrr" };
var prop = "cat";

obj.prop = obj['prop'] = "prrrr"
obj[prop] = obj.cat = obj['cat'] = "meow"
3 Likes

All clear. Thanks a lot!

@justa128 variables don’t work with dot notation. Found that out the hard way.

myObj.checkProp tells JS to look for a checkProp property. It doesn’t find it so it returns undefined.

myObj[checkProp] works because b4acket notation accepts variables. object[variable].

1 Like

I was hung on this problem for while too

simply because I had the value of hasOwnProperty in quotations; as in hasOwnProperty(“this”)

This is how it is shown in the module. The values are quoted even though the keys “gift, pet, and pet” are not quoted in the object.

This was most frustrating because I had followed the module example exactly as it was.

I hope this small piece of advice will save other time.

2 Likes

thanks :slight_smile:

1 Like