* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.6 KiB
2.6 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244ca | 使用对象进行查找 | 1 | https://scrimba.com/c/cdBk8sM | 18373 | using-objects-for-lookups |
--description--
对象和字典一样,可以用来存储键/值对。如果你的数据跟对象一样,你可以用对象来查找你想要的值,而不是使用switch或if/else语句。当你知道你的输入数据在某个范围时,这种查找方式极为有效。
这是简单的反向字母表:
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"
--instructions--
把 switch 语句转化为lookup
对象。使用它来查找val
属性的值,并赋值给result
变量。
--hints--
phoneticLookup("alpha")
应该等于"Adams"
。
assert(phoneticLookup('alpha') === 'Adams');
phoneticLookup("bravo")
应该等于"Boston"
。
assert(phoneticLookup('bravo') === 'Boston');
phoneticLookup("charlie")
应该等于"Chicago"
。
assert(phoneticLookup('charlie') === 'Chicago');
phoneticLookup("delta")
应该等于"Denver"
。
assert(phoneticLookup('delta') === 'Denver');
phoneticLookup("echo")
应该等于"Easy"
。
assert(phoneticLookup('echo') === 'Easy');
phoneticLookup("foxtrot")
应该等于"Frank"
。
assert(phoneticLookup('foxtrot') === 'Frank');
phoneticLookup("")
应该等于undefined
。
assert(typeof phoneticLookup('') === 'undefined');
请不要修改return
语句。
assert(code.match(/return\sresult;/));
请不要使用case
,switch
,或if
语句。
assert(
!/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g, ''))
);
--seed--
--seed-contents--
// 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;
}
phoneticLookup("charlie");
--solutions--
function phoneticLookup(val) {
var result = "";
var lookup = {
alpha: "Adams",
bravo: "Boston",
charlie: "Chicago",
delta: "Denver",
echo: "Easy",
foxtrot: "Frank"
};
result = lookup[val];
return result;
}