--- id: 56533eb9ac21ba0edf2244ca title: Using Objects for Lookups challengeType: 1 videoUrl: https://scrimba.com/c/cdBk8sM forumTopicId: 18373 localeTitle: Использование объектов для поиска --- ## Description
Объекты можно рассматривать как хранилище ключей / значений, например словарь. Если у вас есть табличные данные, вы можете использовать объект для «поиска», а не для оператора switch или цепочки if/else . Это наиболее полезно, когда вы знаете, что ваши входные данные ограничены определенным диапазоном. Вот пример простого обратного алфавитного поиска:
var alpha = {
1: "Z",
2: "Y",
3: "X",
4: "W",
...
24: "С",
25: "В",
26: "A"
};
альфа [2]; // "Y"
альфа [24]; // "C"

var value = 2;
альфа [значение]; // "Y"
## Instructions
Преобразуйте оператор switch в объект под названием lookup . Используйте его, чтобы посмотреть val и назначить связанную строку переменной result .
## Tests
```yml tests: - text: phoneticLookup("alpha") should equal "Adams" testString: assert(phoneticLookup("alpha") === 'Adams'); - text: phoneticLookup("bravo") should equal "Boston" testString: assert(phoneticLookup("bravo") === 'Boston'); - text: phoneticLookup("charlie") should equal "Chicago" testString: assert(phoneticLookup("charlie") === 'Chicago'); - text: phoneticLookup("delta") should equal "Denver" testString: assert(phoneticLookup("delta") === 'Denver'); - text: phoneticLookup("echo") should equal "Easy" testString: assert(phoneticLookup("echo") === 'Easy'); - text: phoneticLookup("foxtrot") should equal "Frank" testString: assert(phoneticLookup("foxtrot") === 'Frank'); - text: phoneticLookup("") should equal undefined testString: assert(typeof phoneticLookup("") === 'undefined'); - text: You should not modify the return statement testString: assert(code.match(/return\sresult;/)); - text: You should not use case, switch, or if statements testString: assert(!/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g,''))); ```
## Challenge Seed
```js // Setup function phoneticLookup(val) { var result = ""; // Only change code below this line switch(val) { case "alpha": result = "Adams"; break; case "bravo": result = "Boston"; break; case "charlie": result = "Chicago"; break; case "delta": result = "Denver"; break; case "echo": result = "Easy"; break; case "foxtrot": result = "Frank"; } // Only change code above this line return result; } // Change this value to test phoneticLookup("charlie"); ```
## Solution
```js function phoneticLookup(val) { var result = ""; var lookup = { alpha: "Adams", bravo: "Boston", charlie: "Chicago", delta: "Denver", echo: "Easy", foxtrot: "Frank" }; result = lookup[val]; return result; } ```