Basic JavaScript - Using Objects for Lookups

I can’t for the life of me figure out what I’m doing wrong here. I looked up the answer and there seems to be very little difference between what I’ve got and what’s shown here: freeCodeCamp Challenge Guide: Using Objects for Lookups

The only difference I can see is that I’m using “const” to declare the lookup object and the example uses “var.”

If I console log result after the function runs (shown here), I get “ReferenceError: result is not defined,” so I know I must be doing something wrong at “result = lookup[val],” but I just can’t figure it out. Any help would be appreciated!

  **Your code so far**
// Setup
function phoneticLookup(val) {
let result = "";

// Only change code below this line
const lookup = {
"alpha": "Adams",
"bravo": "Boston",
"charlie": "Chicago",
"delta": "Denver",
"echo": "Easy",
"foxtrot": "Frank"
};
result = lookup[val];
// Only change code above this line
return result;
}
console.log(result);
console.log(phoneticLookup = ("charlie"));

  **Your browser information:**

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

Challenge: Basic JavaScript - Using Objects for Lookups

Link to the challenge:

Ah, I see now that it’s breaking because I’m console logging result outside of the function. But the only reason I tried logging it in the first place was to troubleshoot why it wasn’t working, so I don’t know what I would have been doing wrong then. Either way, I’ve got it working now!

1 Like

You’re trying log a variable that only exists inside that function

1 Like