Testing Objects for Properties - not understanding

Hi, hope someone can help explain :slight_smile:

I wrote code using the example given on the left hand side of the screen as a guide (below):

var myObj = {
top: "hat",
bottom: "pants"
};
myObj.hasOwnProperty("top"); // true
myObj.hasOwnProperty("middle"); // false

Instructions are: Modify the function checkObj to test myObj for checkProp. If the property is found, return that property’s value. If not, return “Not Found”.

This is the code I wrote:

Your code so far


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

function checkObj(checkProp) {
  // Your Code Here
  
myObj.hasOwnProperty("gift");
myObj.hasOwnProperty("kitten");
myObj.hasOwnProperty("sleigh");

  return "Not Found";
}

// Test your code by modifying these values
checkObj("gift");
  • but it wouldn’t let me pass. I spent ages trying to work out what I did wrong and eventually went to YouTube, where the person explaining how to solved it used an ‘if’ statement - I’m a REAL newbie to all of this so should it have been obvious that I was supposed to use an ‘if’ statement?? Sometimes I’m not sure what I’m learning. What am I missing?

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties

1 Like
  1. You’re not using the checkProp argument. What the challenge wants you to do is to check whether the string passed into the function as checkProp is a property and return a value accordingly.
  2. You always return “Not Found”, so checking the properties doesn’t actually do anything.

Your have to create a function.

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

function checkObj(checkProp) {
if(myObj.hasOwnProperty(checkProp)){
// do something
return // fill this in
}
return “Not Found”;
}
}