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.
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.
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