Using Objects for Lookups - Why does it say lookup is not defined?

Tell us what’s happening:

I clicked on ‘get a hint’ on the challenge page and after typing/copying exactly what the solution is, it continues to tell me:
// running tests

lookup is not defined

Why is this?

Your code so far

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

  var lookup = {
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"

  result = lookup[val];

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:

Your issue seems to be one of scope. Because you declared lookup inside your function, you can’t access it outside.

1 Like

I think you need to move your

result = lookup[val];

Up into your function after your object declaration.

As it stands you are trying to access lookup with result, but result has no access to lookup.

Good luck!

1 Like

To add to what’s been said here, check out MDN’s page on the var keyword. The examples at the top explains some expected behavior, but the Description section has this relevant gem for newbies:

var declarations, wherever they occur, are processed before any code is executed. This is called hoisting, and is discussed further below.

The scope of a variable declared with var is its current execution context , which is either the enclosing function or, for variables declared outside any function, global. If you re-declare a JavaScript variable, it will not lose its value.

To summarize:

  1. If you used var to declare a variable OUTSIDE any function, it’s a global variable, accessible to all code: i.e., inside any functions you declare, or at the “top” level of the code.
  2. If you used var to declare a variable INSIDE a function, it’s only accessible in that function.

All of your code, besides the function call phoneticLookup("charlie"), needs to be encapsulated inside the phoneticLookup(val) function.
As others have mentioned, the problem is that the object-property you’re assigning to the variable (result) is local to the function (i.e. it must be assigned inside the function), and you’re assigning it outside the function :slight_smile: