freeCodeCamp Challenge Guide: Testing Objects for Properties

It took me a while too! (I was not using the .hasOwnProperty …)

Basically you want to build a boolean logic, so you want a true/false answer by the code that you get with an if statement.

So here it checks if the value “checkProp” (in this case it’s “gift”) is a property of myObj.

If yes: return the property of the object

Else: it does not have this property

8 Likes

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

function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];}

else {return “Not Found”;}}
// Test your code by modifying these values
checkObj(“pet”);

This worked for me. I’m just a noob but as I understand it if/ else if ,etc are all Boolean so they are always true/false statements.

6 Likes

I almost had your exact same solution except instead of return myObj[checkProp] I did return myObject.checkProp which doesn’t pass. Do you happen to know why that is? Thanks in advance!

24 Likes

I was having the same issue, not sure why dot notation doesn’t pass.

9 Likes

try this

function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp) === true) {
return myObj[checkProp];
}
return “Not Found”;

6 Likes

Here’s what I’ve written. Kindly put some comments to improve my code. Thanks!

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

function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp];
} else {
return “Not Found”;
}

return “Change Me!”;
}

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

3 Likes

Phew, I’m glad most of us came up with similar answers. At this point, FCC seems to be giving us room to think on our own without all the hand-holding. This is what I came up with.

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

Try this

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

function checkObj(checkProp) {
// Your Code Here
return myObj.hasOwnProperty(checkProp) === true ? myObj[checkProp] : “Not Found”;
}

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

7 Likes

I used a var string result to make it easier to understand

It was not necessary and could be made shorter, but it helps you understand the context a little bit better.

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

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

Below is the shortest answer possible using a ternary operator in javascript. See here:

Essentially, the format is condition ? "value if true" : "value if false" .

Basicaly, if the condition is true, output the “value if true” , if its false “value if false”

The “Condition” must result in a true or false boolean value.

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

function checkObj(checkProp) {
  // Your Code Here
  return hasownProperty(checkProp) ? myObj[checkProp] : "Not Found";
}
// Test your code by modifying these values
checkObj("gift");
20 Likes

As the property name is stored in the variable we need to use the [ ].

12 Likes

why can’t we use . instead of [ ] ?
I was having problem getting this resolved because I kept using return myObj.checkProp.
Any guidance is appreciated!

5 Likes

@TanikaR @ittakesii Did you guys figure out what it wouldn’t work using the dot notation? Trying to understand the reason. Thanks!

2 Likes

thanks. I am not sure I understand. Can you rephrase it?

3 Likes

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

function checkObj(checkProp) {
if(myObj.hasOwnProperty(checkProp))
return myObj[checkProp];
return ‘Not Found’;
}

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

3 Likes

I have the same question.

1 Like
if (myObj.hasOwnProperty(checkProp)) { 
    return myObj[checkProp];
}
else return "Not Found";

4 Likes

Hi codecampers!

I didn’t have much trouble with this, but why this isn’t working?

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

function checkObj(checkProp) {
if(myObj.hasOwnProperty(checkProp)){
return myObj.checkProp;
}else{
return “Not Found”;
}

return “Change Me!”;
}

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

3 Likes

I used this simple solution

// 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”);

2 Likes

I’m a little bit frustrated if I’m honest.

I’m understanding a lot of the logic and I knew this needed boolean to solve and in fact my answer contained pretty much all of what was required - so I’m doing something correctly, however, I’m causing myself many problems with arguments/parameters and for some reason I can’t quite fathom which argument to pass through whatever function.

When I mentioned I had the answer, I did but I also created a for loop, thinking I needed to iterate through myObj and then, I was passing the for loop (i) through myObj and when that didn’t work I tried passing checkProp through, which is obviously correct but the rest of my code was wrong because of the loop so if failed but it’s the arguments that are really killing me.

I’ve learned a lot and I feel I’ve made good progress - take away the argument issue and it’s all making sense, well mostly but passing arguments through things is making me feel lost again.

Has anyone else struggled with arguments and what did you do to overcome them and fully understand them?

4 Likes

What I don’t understand is how checkProp comes into play. It doesn’t look like it’s defined. I see that checkProp corresponds to the object property.
Does if(myObj.hasOwnProperty(checkProp)) basically check if an object has a property, then returns True or False?

10 Likes