Am Stuck with My Challenge Code | checking obj if checkProp property exists!

Tell us what’s happening:
Am having a challenge to check if a property exists in an object but using a function as shown in the code below. tried some hints but when i alert the console.log() to give me an output, its first said:

  1. obj not defined - (i tried to define it by creating some object obj then that was sorted)

  2. checkProp not defined , now thats where am stuck because the initial code before i started defining the obj, was
    /*
    if(obj.hasOwnProperty(“checkProp”)){
    return obj[checkProp];
    } else {return “Not Found”;}
    */

So help me there.

Your code so far


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

console.log(obj,hasOwnProperty(obj, checkProp));
   **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36

Challenge: Testing Objects for Properties

Link to the challenge:

Well, first of all checkProp is a VARIABLE and you just turned it into a STRING in the if condition!

Hello there, there seem to be some things going wrong here. No big deal, everyone makes mistakes.

  1. You don’t need to define the obj, because it is already referring to an object, even though it doesn’t show on screen. You can trust on that.
if(obj.hasOwnProperty("checkProp")){
 return obj[checkProp];
} else {return "Not Found";}
// Only change code above this line
}

This is not bad, only problem is checkProp is now a string, because you put it inside quotes. If you remove the quotes you will see that checkProp is one of the arguments of the function, next to obj.

Hope this is helpful.

tried it but still the same problem

well the problem could be the console.log() because when i comment it, it executes well.

thanks a million guys.

i appreciates it.

1 Like

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