Testing Objects for Properties Stuck

Testing Objects for Properties Stuck
0

#1

Can someone tell me why this doesn’t work?

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

function checkObj(checkProp) {
  
  if (myObj.hasOwnProperty(checkProp)) {
    return myObj.checkProp;
  } else {
  return "Not found";
  }
}

checkObj("gift");

Also I don’t know how to make this appear as code in this box…?


#2

You are using dot notation which assumes myObj has an actual property named “checkProp”. It does not, so your if statement returns undefined instead of the value of the property.

You should review the following lesson.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables/


#3

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#4
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  
  if (myObj.hasOwnProperty(checkProp)===true) {
    return myObj[checkProp];
  } else  {
  return "Not found";
  }
}

checkObj("gift");

Now I’m getting an error saying

checkObj("house") should return  "Not Found".

#5

Yes, it should return “Not Found”. Your solution is returning “Not found” which is different than what the instructions ask. JavaScript is case-sensitive.