Testing Objects for Properties

Solutions

Solution 1 (Click to Show/Hide)
``````
function checkObj(obj, checkProp) {
if(obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
}
}
``````
36 Likes

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

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

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

// Test your code by modifying these values

19 Likes

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 = {
pet: “kitten”,
bed: “sleigh”
};
/

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

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

16 Likes

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 = {
pet: “kitten”,
bed: “sleigh”
};

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

}

// Test your code by modifying these values

34 Likes

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

26 Likes

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 = {
pet: “kitten”,
bed: “sleigh”
};

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

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

22 Likes

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

8 Likes

try this

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

6 Likes

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

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

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

return “Change Me!”;
}

// Test your code by modifying these values

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 {
}
}``````
5 Likes

Try this

var myObj = {
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

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 = {
pet: "kitten",
bed: "sleigh"
};

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

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 = {
pet: "kitten",
bed: "sleigh"
};

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

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

10 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!

3 Likes

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

1 Like

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

3 Likes

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

function checkObj(checkProp) {
if(myObj.hasOwnProperty(checkProp))
return myObj[checkProp];