Basic JavaScript - Using Objects for Lookups

Tell us what’s happening:
Objects can be thought of as a key/value storage, like a dictionary. If you have tabular data, you can use an object to lookup values rather than a switch statement or an if/else chain. This is most useful when you know that your input data is limited to a certain range.

That is what I am told to do. This seemed like the most straight forward lesson so far for JavaScript, yet I have no idea what I could have done wrong. I looked at forum posts from 2017 with this exact code on it that passes but in 2023 it does not. Very confused…

Your code so far

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

  // Only change code below this line
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"
  };
  // Only change code above this line
  return result;
}

phoneticLookup("charlie");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Using Objects for Lookups

Link to the challenge:

Copying old answers will usually result in failing the challenge since we update them over the years.

I wouldn’t ever copy answers you find.

You never changed ‘result’, so you are returning an empty string here. You need to use the lookup object to update the value of ‘result’

This is malformed syntax for declaring a variable that holds an object. You need a let or const and a variable name, as well as an opening {

Hey Jeremy, after taking a break from frustration I am coming back to this and still completely stumped. You said I need to change the ‘return result;’ line of code to use the lookup object to update the value of ‘result’. But looking at the code, where I would update the ‘return result;’ is outside of the area of code I am supposed to change for the exercise. I am only supposed to change below ‘let result = “”,’ and above ‘return result;’, so how is this supposed to be properly done?

I don’t normally look for the answers, but this one stumped me so bad I HAD to find what I was doing wrong, to only find out I basically matched the correct answer from 2017 and it still did not pass.

As far as the malformed syntax for declaring a variable, I am matching the EXACT format they use for the example. So I am stumped again on that one…

Declare a new variable ‘const’, give it a name, and assign the existing object to it. Then, assign that variable with a ‘val’ argument (use the bracket notation) to the ‘result’ variable:

existingVar = newVar[val]; ... this is guidance

Check the output by applying the console.log() method:

console.log(phoneticLookup("echo")); ...for example

The printed value is :
image

Not quite. You need to change the contents of the result variable.

But you did not match the answer at all. Syntax is pretty tricky. You need to get the syntax correct or it definitely doesn’t work. Unfortunately here the syntax is definitely not correct.

You really aren’t. You are doing something that looks close but isn’t valid syntax.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.