2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 56533eb9ac21ba0edf2244ca
|
|
|
|
|
title: Using Objects for Lookups
|
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cdBk8sM'
|
|
|
|
|
forumTopicId: 18373
|
2018-10-10 18:03:03 -04:00
|
|
|
|
localeTitle: 使用对象进行查找
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
对象和字典一样,可以用来存储键/值对。如果你的数据跟对象一样,你可以用对象来查找你想要的值,而不是使用switch或if/else语句。当你知道你的输入数据在某个范围时,这种查找方式极为有效。
|
|
|
|
|
这是简单的反向字母表:
|
|
|
|
|
|
|
|
|
|
```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"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
把 switch 语句转化为<code>lookup</code>对象。使用它来查找<code>val</code>属性的值,并赋值给<code>result</code>变量。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>phoneticLookup("alpha")</code>应该等于<code>"Adams"</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(phoneticLookup("alpha") === 'Adams');
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>phoneticLookup("bravo")</code>应该等于<code>"Boston"</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(phoneticLookup("bravo") === 'Boston');
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>phoneticLookup("charlie")</code>应该等于<code>"Chicago"</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(phoneticLookup("charlie") === 'Chicago');
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>phoneticLookup("delta")</code>应该等于<code>"Denver"</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(phoneticLookup("delta") === 'Denver');
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>phoneticLookup("echo")</code>应该等于<code>"Easy"</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(phoneticLookup("echo") === 'Easy');
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>phoneticLookup("foxtrot")</code>应该等于<code>"Frank"</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(phoneticLookup("foxtrot") === 'Frank');
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>phoneticLookup("")</code>应该等于<code>undefined</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(typeof phoneticLookup("") === 'undefined');
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 请不要修改<code>return</code>语句。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/return\sresult;/));
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 请不要使用<code>case</code>,<code>switch</code>,或<code>if</code>语句。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(!/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g,'')));
|
2018-10-10 18:03:03 -04: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) {
|
|
|
|
|
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");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
|
function phoneticLookup(val) {
|
|
|
|
|
var result = "";
|
|
|
|
|
|
|
|
|
|
var lookup = {
|
|
|
|
|
alpha: "Adams",
|
|
|
|
|
bravo: "Boston",
|
|
|
|
|
charlie: "Chicago",
|
|
|
|
|
delta: "Denver",
|
|
|
|
|
echo: "Easy",
|
|
|
|
|
foxtrot: "Frank"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result = lookup[val];
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|