Testing Objects for Properties Challenge Difficulties

Testing Objects for Properties Challenge Difficulties
0

#1

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.


#2

In according to hasOwnProperty :

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

#3
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.


#4

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:


#5

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

#6

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”.


#7

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.


#8

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”


#9

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:


#10

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


#11

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.


#12

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


#13

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

#14

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


#15

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


#16

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"

#17

All clear. Thanks a lot!


#18

@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].


#19

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.


#20

thanks :slight_smile: