JavaScript Testing Objects for Properties

Tell us what’s happening:
I can’t get my code to pass for the else statements and I keep getting an “Unexpected token” error on my else. I’m not sure what I am doing wrong. Can anyone help me figure out my error(s)?

Your code so far


var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh",
  city: "Seattle"
};

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

**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36</code>.

**Challenge:** Testing Objects for Properties

**Link to the challenge:**
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties

you have an extra ; after the if condition

remove that to solve the syntax error

it will not pass the challenge tho

Thanks ieahleen! Now almost everything passes except the final condition. Here is my code at present

var myObj = {

gift: “pony”,

pet: “kitten”,

bed: “sleigh”,

city: “Seattle”

};

function checkObj(obj, checkProp) {

// Only change code below this line

if (myObj.hasOwnProperty(checkProp)) {

return myObj[checkProp];

} else {

return “Not Found”;}

// Only change code above this line

}

This is the condition that is not passing: checkObj({pet: "kitten", bed: "sleigh"}, "gift") should return "Not Found" .

You’ve written a function that takes two parameters: obj and checkProp.

Why is obj passed to the function, what’s the purpose?

jsdisco I’m not sure. That was the line already included from FreeCodeCamp. But when I remove obj and just have checkProp it makes less of the conditions pass.

I see my error now. I should have myObj rather than obj there. There helps everything pass! Thanks for the tip!

My final code that passed was:

var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”,
city: “Seattle”
};

function checkObj(myObj, checkProp) {
// Only change code below this line
if (myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp];
} else {
return “Not Found”;}

// Only change code above this line
}

That might pass the tests, but it’s still a little off - are you aware that you can just delete the object myObj that you’ve created, and the tests will still pass?