Object lookup question

Object lookup question
0

#1

Hey, I was wondering why bracket notation works for the situation below, when reassigning result to the lookUp object. However if I use Dot notation, the code does not work.

You can find the challenge here:


// 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];


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

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

To be more specific. I am talking about bracket notation

result = lookUp[val];

vs dot notation

result = lookUp.val;

and why dot notation does not work for the challenge. At Least the way I implemented it.


#2

dot notation cannot use a variable, it looks for that name. So in essence you were trying to get:

lookup = {
  val: undefined
};

lookup.val =  undefined

to use a variable in lookup you need to use the associative array notation:

val = alpha;
lookup = {
  alpha = omega
}

lookup[val] = omega

MDN explanation

Stackoverflow Answer


#3

Oh, I get it. Thanks, thought it might be my fault.:+1::pray: