switch
语句或if/else
链。当您知道输入数据限制在特定范围内时,这非常有用。以下是简单反向字母查找的示例: var alpha = {
1: “Z”,
2: “Y”,
3: “X”,
4: “W”,
...
24: “C”,
25: “B”,
26: “A”
};
阿尔法[2]; //“Y”
阿尔法[24]; // “C”
var value = 2;
阿尔法[值]。 //“Y”
lookup
的对象。使用它来查找val
并将关联的字符串分配给result
变量。 phoneticLookup("alpha")
应该等于"Adams"
testString: assert(phoneticLookup("alpha") === 'Adams');
- text: phoneticLookup("bravo")
应该等于"Boston"
testString: assert(phoneticLookup("bravo") === 'Boston');
- text: phoneticLookup("charlie")
应该等于"Chicago"
testString: assert(phoneticLookup("charlie") === 'Chicago');
- text: phoneticLookup("delta")
应该等于"Denver"
testString: assert(phoneticLookup("delta") === 'Denver');
- text: phoneticLookup("echo")
应该等于"Easy"
testString: assert(phoneticLookup("echo") === 'Easy');
- text: phoneticLookup("foxtrot")
应该等于"Frank"
testString: assert(phoneticLookup("foxtrot") === 'Frank');
- text: phoneticLookup("")
应该等于undefined
testString: assert(typeof phoneticLookup("") === 'undefined');
- text: 您不应该修改return
语句
testString: assert(code.match(/return\sresult;/));
- text: 您不应该使用case
, switch
或if
语句
testString: assert(!/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g,'')));
```