freeCodeCamp Challenge Guide: Testing Objects for Properties

freeCodeCamp Challenge Guide: Testing Objects for Properties
0

#1

Sometimes it is useful to check if the property of a given object exists or not. We can use the .hasOwnProperty([propname]) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.

Example

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

#2

This is how solved it.
must be a better way but it works.

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

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

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


#3

Wow good solve. I couldn’t figure this one out so what I did was just forget the var myObj and create a switch with a default response.

Here is the code:

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

function checkObj(checkProp) {
var result = “”;
switch(checkProp) {
case “gift”:
result = “pony”;
break;
case “pet”:
result = “kitten”;
break;
case “bed”:
result = “sleigh”;
break;
default:
result = “Not Found”;
break;
}
return result;
}

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


#4

Basically the same but there is no point checking if it is not true with a if statement. Since if it not true in the first if statement it has to be false.
// 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(“gift”);


#5

This is a great solution! Can you help me understand the rationale that led you here? I can see exactly what you did, but I’m having a hard time understanding how to arrive at these conclusions on my own.

Thanks!

Brian


#7

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

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


#9

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!


#11

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


#12

try this

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


#13

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


#14

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

#15

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


#16

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

#17

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


#18

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!


#19

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


#20

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


#21

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


#22

I have the same question.