Testing For Object Properties

Tell us what’s happening:
The Last test won’t pass –

checkObj({pet: "kitten", bed: "sleigh"}, "gift")

should return the string

Not Found

I’m not sure what is wrong with this current setup?

  **Your code so far**

function checkObj(obj, checkProp) {
// Only change code below this line
if (obj, checkProp == "gift") {
  return "pony"
} else if (obj, checkProp == "pet") {
  return "kitten"
} else if (obj, checkProp == "bed") {
  return "sleigh"
} else if (obj, checkProp == "city") {
  return "Seattle"
}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; rv:100.0) Gecko/20100101 Firefox/100.0

Challenge: Testing Objects for Properties

Link to the challenge:

1, obj.hasOwnProperty(prop) will return true if the property is present or false otherwise;
2, use bracket notation to access property value e.g obj[“property”]

if(obj.hasOwnProperty(prop)) {
   return  use bracket notation to access property value;
return "Not Found"

@Jonesx87 What do you think the above line is checking? Describe in your own words.

If obj and checkProp is equal to “gift”

So you mean you think it checks if both obj and checkProp are equal to “gift” or do you think it checks if obj is an object and checkProp is equal to “gift”?

I can tell you the statement checks neither of these scenarios. Where have you seen the use of a comma in a logical comparison statement?

Putting that comma in after obj causes JavaScript to only return a value for the 2nd operand (checkProp == "gift") , so in the last test case, since checkProp is “gift”, the if statement evaluates to true causing “pony” to be returned.

Per the instructions, you need to validate two things:

  1. obj passed to the function is an object
  2. If the property exists, return the value of that property of obj

You are attempting to hard code the property values instead of using an algorithm to get the property’s value if it is found in the object.

Thank you that gave me a hint to solve it!

Thank you for pointing out the (checkProp == "gift). I hadn’t realized it was only checking the one part of it.