freeCodeCamp Challenge Guide: Testing Objects for Properties

freeCodeCamp Challenge Guide: Testing Objects for Properties
0

Testing Objects for Properties


Solutions

Solution 1 (Click to Show/Hide)

We do not change anything here:

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

further, in the body of the function we use .hasOwnProperty(propname) method of objects to determine if that object has the given property name. if/else statement with Boolean Values will help us in this:

function checkObj(checkProp) {
  // Your Code Here
  if (myObj.hasOwnProperty(checkProp) == true) {
    return myObj[checkProp];
  }
  else {
 //  and change the value of `return` in `else` statement:
 
    return "Not Found"
  }
}

Now, you can change checkObj values:

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

Here’s a full solution:

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

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

11 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 = {
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”);

11 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 = {
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”);

29 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

17 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

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

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

16 Likes

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

4 Likes

try this

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

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

2 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";
  }
}
3 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”);

5 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");
18 Likes

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

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

2 Likes

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

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

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

1 Like

I have the same question.