Whats wrong with this?

Tell us what’s happening:
the code should run as i think i completed the requirements…but still its not working

Your code so far



//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";
}
}
console.log(checkObj("gift"));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0.

Challenge: Testing Objects for Properties

Link to the challenge:

The function checkObj() takes 2 parameters (obj, checkProp), but you are only passing one.

You don’t need to define the object yourself, it is passed as a parameter.

2 Likes
function checkObj(obj, checkProp) {
  if(obj.hasOwnProperty(checkProp)){
    return obj[checkProp];
  }
  return "Not Found";
}

You don’t have to write the else block as the second return will only run if the if block equates to false.

Also as Gilbert said you need to have the obj as a parameter.

lots of thanks…it worked :slight_smile:

thanks alot it worked:)

You can do this in just a single line with the es6 syntax,

const checkObj = (obj,checkProp) => obj.hasOwnProperty(checkProp) ? obj[checkProp] : "Not Found";

This obviouly comes at the expense of readability, but the overall code becomes less.

1 Like