Testing Objects for Properties error

Tell us what’s happening:
Please help me i have solved this with if else and also created another
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
}

because in my case its not available so i needed to create one
after solving this as:-
var answer = “”;
if(myObj.hasOwnProperty[checkProp]){
answer = myobj[checkProp];
}
else {
return “Not Found”;
}

but my code here dosent work only the not found ‘else’ statement is working not the if statement please help ASAP!!!

Your code so far


function checkObj(obj, checkProp) {
// Only change code below this line
return "Change Me!";
// 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/86.0.4240.183 Safari/537.36.

Challenge: Testing Objects for Properties

Link to the challenge:

you do not have a return statement here

but also:

you are not using the obj parameter. Delete the myObj object and any references to it, and just use the obj parameter

Hi and welcome to the forum! :slight_smile:

I’m not sure I got the whole picture, I assume that your function looks like this:

function checkObj(obj, checkProp) {
  var answer = "";
  if(myObj.hasOwnProperty[checkProp]) {
    answer = myobj[checkProp];
  else {
    return “Not Found”;
  }
}

There are couple things that are wrong.

  1. hasOwnProperty is a method (function), so you pass an argument to it like this
obj.hasOwnProperty(checkProp)
  1. In your function you’re referring to some hardcoded object, the point is to use the object that is passed as an argument.
  2. You have to decide whether you want to assign something to an answer and by the end of the function return the answer or just return the value inside if statement.
function foo() {
  var answer = "";
  if (condition) {
    answer = "option 1";
  else {
    answer = "option 2";
  }
  return answer;
}

function bar() {
  if (condition) {
    return "option 1";
  else {
    return "option 2";
  }
}