Using Objects for Lookups it does not work

What am I doing wrong ? I have seen many examples and I do not see how to understand Using Objects for Lookups.
Objects can be considered as key / value storage, like a dictionary. If you have tabular data, you can use an object to “search” for values instead of a statement switch or an if / elsechain. This is most useful when you know that your input data is limited to a certain range.
Can you help me understand it? My code does not work …
Instructions

Convert the switch statement into a lookup table called lookup. Use it to lookup val and assign the associated string to the result variable.

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

  // Only change code below this line
  var lookup= {

      alpha:"Adams",

      bravo: "Boston",
 
      charlie: "Chicago",

      delta:"Denver",
  
     eco: "Easy",
 
      "foxtrot": "Frank",
    result : lookup[val]
  };

  // Only change code above this line
  return result;
}

// Change this value to test
phoneticLookup("charlie");

VM216:19 Uncaught TypeError: Cannot read property 'charlie' of undefined
    at phoneticLookup (<anonymous>:19:20)
    at <anonymous>:27:1

Hey @miguelito! You almost there, I’ll give you a small suggestion here.
Look closely at your lookup object (you use it as a dictionary here). It has to do just one thing - store the data.
After that, you should assign the value to the variable result. To do that you should use the syntax lookup[val] as you do, but it shouldn’t be in the object, it’ll be different operation.
Please try to separate them and if you have further questions, feel free to tag me out.

2 Likes

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

2 Likes

I still do not understand.
Change result, but it does not work I do not understand it.
result : val.lookup

Thank you for the support and understanding.
I should check before the link that you propose and learn more about the site.

You was trying to do that inside the object. You cannot assign variable inside the object. You should try to do that outside of the curly braces

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

  // Only change code below this line
  var lookup= {

      "alpha":"Adams",

      "bravo": "Boston",
 
      "charlie": "Chicago",

      "delta":"Denver",
  
     "eco": "Easy",
 
      "foxtrot": "Frank",
    
  };
  
 result = val.lookup;

  // Only change code above this line
  return result;
}

// Change this value to test
phoneticLookup("charlie"); ```

It does not work, I do not understand

What’s the reason did you change lookup[val] on val.lookup;?
lookup[val] was correct syntax.

1 Like

You are right, I’m wrong. But it still fails, it must return:
phoneticLookup(“echo”) should equal “Easy”.
It is the only one that fails

function phoneticLookup(val) {
var result = “”;

var lookup= {

  "alpha":"Adams",

  "bravo": "Boston",

  "charlie": "Chicago",

  "delta":"Denver",

 "echo": "Easy",

  "foxtrot": "Frank",

};

result = lookup[val];

return result;
}

phoneticLookup("");

That’s because you have a typo error in the word echo. Your said eco

That was the problem, which would be the programming without the typographical errors … thanks

Just detach a little from the code and after you will see the error.

3 Likes