Using Objects for Lookups why can't i access property in object using dot operator

Tell us what’s happening:

Your code so far


// Setup
function phoneticLookup(val) {
  var result = "";

  // Only change code below this line
  var lookup= {
    alpha:"Adams",
    bravo:"Boston",
    charlie: "Chicago",
    delta:"Denver",
    echo: "Easy",
    foxtrot: "Frank"
  }
  result=lookup.val;

> //it is not working when i use dot operator why?

// Only change code above this line
  return result;
}

// Change this value to test
phoneticLookup("charlie");

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups

Blockquote

There isn’t a property called val on that object.

> myObject = { someProperty: "hello" };
> var prop = "someProperty"
> myObject.prop
# there isn't a property called prop, so do this:
> myObject[prop]
# that evaluates what `prop` is - so it checks it, sees `prop` is
# a string, the string is "someProperty", look up `myObject.someProperty`

If what you’ve written could work, as a thought experiment, what would happen if that lookup object has a property called val? Which would win, the value of that val you passed in as the argument to the function or the val that’s already on the object?

1 Like