Link Search Menu Expand Document

Using Objects for Lookups

Summary

  • An object can be used rather than an if/else or switch statement.

Final Code

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

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

phoneticLookup("charlie");