Using Objects for Lookups - Will someone please explain why this works?

Using Objects for Lookups - Will someone please explain why this works?
0.0 0

#1

Hey All,

I am not sure I understand why the code below works.

In particular, how does the function know what value to pass through the lookup list (alpha, charlie, bravo…)? Where is it stated that val = parameter?

Very confused…

// 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",
  };
  return lookup[val];

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

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

Link to the challenge:


#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Right here: function phoneticLookup(val)
This is saying "whatever gets passed in to the function phoneticLookup is the parameter val"


#4

I suggest you go back and review the following challenge which should clear up how to pass values to functions.


#5

Ok, I see!

A function wouldn’t necessarily have to precede an object, right?


#6

If you declared the object outside of the function, then it would be a global variable. Remember this?