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

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:
https://www.freecodecamp.org/challenges/using-objects-for-lookups

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.

markdown_Forums

1 Like

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

Ok, I see!

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

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