Hi guys didnt understand the testing object

really didnt understand can you please explain like if i am a 5 year old

   **Your code so far**

function checkObj(obj, checkProp) {
 // Only change code below this line
if(obj.hasOwnProperty(checkProp)) {
  return obj[checkProp];

} else{
  return "Not Found";
 // Only change code above this line
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Testing Objects for Properties

Link to the challenge:

why we check obj with checkprop if they are 2 different parameter ?

Hi @rejdi93 !

I added spoiler tags around your code since it is a full working solution.

I am not sure fully understand your question but I’ll try to explain what is going on here.

We have an object that has two parameters.
obj represents the object passed into the function.
checkProp represents the property name that we are checking to see if it exists in the object.

Let’s take a look at a function call

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")

This if statement

if(obj.hasOwnProperty(checkProp)) {
  return obj[checkProp];

is saying, if "gift" exisits in {gift: "pony", pet: "kitten", bed: "sleigh"}, then return the value.

We can see that the gift property does exist

gift: "pony"

so we return the value

Let’s look at another example

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")

We first check if "house" exists in {gift: "pony", pet: "kitten", bed: "sleigh"}
We can look through the object and clearly see that the "house" property does not exist so we execute the else block

  return "Not Found";

Hope that makes sense!

yes i think i understand now after visualizing the obj with the property/value
i have one more question …what happens if instead of the property we call the value ( pony ) does have the same result or it works only if we call the property?
thanks for the help

If you wanted to check for values instead of the property then you would take a different approach.

This could be one way to do that. I provided comments to help explain the code.

Using this function call

checkObj({ gift: "pony", pet: "kitten", bed: "sleigh" }, "pony");

the code below would return "pony"

function checkObj(obj, checkValue) {
// Object.values creates an array of all the values in the object
  let valuesArr = Object.values(obj); 

// the includes array method will check if the value exists in the valuesArr
  if (valuesArr.includes(checkValue)) {
    return checkValue;
  } else {
    console.log("Not Found");
    return "Not Found";
checkObj({ gift: "pony", pet: "kitten", bed: "sleigh" }, "pony");

Here are the links to the documentation for the includes method and Object.values
There are other approaches but that is how I would check if the value existed.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.