switch
语句写起来会比多个串联的if
/if else
语句容易些,譬如:
```js
if (val === 1) {
answer = "a";
} else if (val === 2) {
answer = "b";
} else {
answer = "c";
}
```
可以被下面替代:
```js
switch(val) {
case 1:
answer = "a";
break;
case 2:
answer = "b";
break;
default:
answer = "c";
}
```
if
/if else
语句改成switch
语句。
else
表达式。
testString: assert(!/else/g.test(code));
- text: 不要使用if
表达式。
testString: assert(!/if/g.test(code));
- text: 你应该有至少 4 个break
表达式。
testString: assert(code.match(/break/g).length >= 4);
- text: chainToSwitch("bob")
应该为 "Marley"。
testString: assert(chainToSwitch("bob") === "Marley");
- text: chainToSwitch(42)
应该为 "The Answer"。
testString: assert(chainToSwitch(42) === "The Answer");
- text: chainToSwitch(1)
应该为 "There is no #1"。
testString: "assert(chainToSwitch(1) === \"There is no #1\");"
- text: chainToSwitch(99)
应该为 "Missed me by this much!"。
testString: assert(chainToSwitch(99) === "Missed me by this much!");
- text: chainToSwitch(7)
应该为 "Ate Nine"。
testString: assert(chainToSwitch(7) === "Ate Nine");
- text: chainToSwitch("John")
应该为 "" (empty string)。
testString: assert(chainToSwitch("John") === "");
- text: chainToSwitch(156)
应该为 "" (empty string)。
testString: assert(chainToSwitch(156) === "");
```