Testing Objects for Properties dot notation error

Tell us what’s happening:

Is there an issue with the logic behind since myObj.checkProp doesn’t work, only myObj[checkProp].

Dot notation should be equally acceptable or there is something else here ?

Thanks

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";
  }

}

// Test your code by modifying these values
checkObj("gift");

Your browser information:

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

Link to the challenge:
/javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties

checkObj is not a function inside myObj, so you can’t use it with dot notation.

var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh",
  checkObj: (checkProp) => {
  if (myObj.hasOwnProperty(checkProp)) {
    return myObj[checkProp];
    }
    else {
      return "Not Found";
    }
  }
}

Oh, that’s the reason, now it makes sense. Thanks!

This will be informative:

1 Like