2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244ca
title: Using Objects for Lookups
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cdBk8sM'
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
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 < code > switch< / code > statement or an < code > if/else< / code > chain. This is most useful when you know that your input data is limited to a certain range.
Here is an example of a simple reverse alphabet lookup:
2019-05-17 06:20:30 -07:00
```js
var alpha = {
1:"Z",
2:"Y",
3:"X",
4:"W",
...
24:"C",
25:"B",
26:"A"
};
alpha[2]; // "Y"
alpha[24]; // "C"
var value = 2;
alpha[value]; // "Y"
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
Convert the switch statement into an object called < code > lookup< / code > . Use it to look up < code > val< / code > and assign the associated string to the < code > result< / code > variable.
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: < code > phoneticLookup("alpha")</ code > should equal < code > "Adams"</ code >
2018-10-20 21:02:47 +03:00
testString: assert(phoneticLookup("alpha") === 'Adams', '< code > phoneticLookup("alpha")< / code > should equal < code > "Adams"< / code > ');
2018-10-04 14:37:37 +01:00
- text: < code > phoneticLookup("bravo")</ code > should equal < code > "Boston"</ code >
2018-10-20 21:02:47 +03:00
testString: assert(phoneticLookup("bravo") === 'Boston', '< code > phoneticLookup("bravo")< / code > should equal < code > "Boston"< / code > ');
2018-10-04 14:37:37 +01:00
- text: < code > phoneticLookup("charlie")</ code > should equal < code > "Chicago"</ code >
2018-10-20 21:02:47 +03:00
testString: assert(phoneticLookup("charlie") === 'Chicago', '< code > phoneticLookup("charlie")< / code > should equal < code > "Chicago"< / code > ');
2018-10-04 14:37:37 +01:00
- text: < code > phoneticLookup("delta")</ code > should equal < code > "Denver"</ code >
2018-10-20 21:02:47 +03:00
testString: assert(phoneticLookup("delta") === 'Denver', '< code > phoneticLookup("delta")< / code > should equal < code > "Denver"< / code > ');
2018-10-04 14:37:37 +01:00
- text: < code > phoneticLookup("echo")</ code > should equal < code > "Easy"</ code >
2018-10-20 21:02:47 +03:00
testString: assert(phoneticLookup("echo") === 'Easy', '< code > phoneticLookup("echo")< / code > should equal < code > "Easy"< / code > ');
2018-10-04 14:37:37 +01:00
- text: < code > phoneticLookup("foxtrot")</ code > should equal < code > "Frank"</ code >
2018-10-20 21:02:47 +03:00
testString: assert(phoneticLookup("foxtrot") === 'Frank', '< code > phoneticLookup("foxtrot")< / code > should equal < code > "Frank"< / code > ');
2018-10-04 14:37:37 +01:00
- text: < code > phoneticLookup("")</ code > should equal < code > undefined</ code >
2018-10-20 21:02:47 +03:00
testString: assert(typeof phoneticLookup("") === 'undefined', '< code > phoneticLookup("")< / code > should equal < code > undefined< / code > ');
2018-10-04 14:37:37 +01:00
- text: You should not modify the < code > return</ code > statement
2018-10-20 21:02:47 +03:00
testString: assert(code.match(/return\sresult;/), 'You should not modify the < code > return</ code > statement');
- text: You should not use < code > case</ code > , < code > switch</ code > , or < code > if</ code > statements
testString: assert(!/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g,'')), 'You should not use < code > case< / code > , < code > switch< / code > , or < code > if< / code > statements');
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// Setup
function phoneticLookup(val) {
var result = "";
// Only change code below this line
switch(val) {
2018-10-08 01:01:53 +01:00
case "alpha":
2018-09-30 23:01:58 +01:00
result = "Adams";
break;
2018-10-08 01:01:53 +01:00
case "bravo":
2018-09-30 23:01:58 +01:00
result = "Boston";
break;
2018-10-08 01:01:53 +01:00
case "charlie":
2018-09-30 23:01:58 +01:00
result = "Chicago";
break;
2018-10-08 01:01:53 +01:00
case "delta":
2018-09-30 23:01:58 +01:00
result = "Denver";
break;
2018-10-08 01:01:53 +01:00
case "echo":
2018-09-30 23:01:58 +01:00
result = "Easy";
break;
2018-10-08 01:01:53 +01:00
case "foxtrot":
2018-09-30 23:01:58 +01:00
result = "Frank";
}
// Only change code above this line
return result;
}
// Change this value to test
phoneticLookup("charlie");
```
< / div >
< / section >
## Solution
< section id = '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;
}
```
< / section >