How to Resolve - Basic JavaScript: Testing Objects for Properties

How to solve this
Modify the function checkObj to test if an object passed to the function ( obj ) contains a specific property ( checkProp ). If the property is found, return that property’s value. If not, return "Not Found" .

function checkObj(obj, checkProp) {
  // Only change code below this line
  return "Change Me!";
  // Only change code above this line
}

My Program

function checkObj(obj, checkProp) {
  // Only change code below this line
  var myObj={
    gift:"pony",
    pet:"kitten",
    bed: "Sleigh"
  }
  return "Change Me!";
  // Only change code above this line
}
checkObj(myObj,"gift");

Getting error
ReferenceError: myObj is not defined

2 Likes

It’s because myobj is not defined.

You do have myObj defined though.

Remember., Javascript is case sensitive.

2 Likes

You do not want to add a object myObj in the middle of the function. That is a sample object from the sample solution that does not go inside of the function.

The challenge is asking you to check object obj for the property checkProp

There is an example in the challenge that shows how to determine if an object has a specific property. Is there something about the example that we can clarify?

2 Likes

How we will Verify this output

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") should return "pony".
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet") should return "kitten".
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house") should return "Not Found".
checkObj({city: "Seattle"}, "city") should return "Seattle".
checkObj({city: "Seattle"}, "district") should return "Not Found".
2 Likes

The tests do that for you. You just need to make sure that your function works.

1 Like

I did it, Thanks

function checkObj(obj, checkProp) {
  // Only change code below this line
  if(obj.hasOwnProperty(checkProp)){
    return obj[checkProp];
  }
  else{
return "Not Found";
  }
3 Likes