switch
语句比许多链接的if
/ else if
语句更容易编写。下列: if(val === 1){可以替换为:
answer =“a”;
} else if(val === 2){
answer =“b”;
} else {
answer =“c”;
}
switch(val){
情况1:
answer =“a”;
打破;
案例2:
answer =“b”;
打破;
默认:
answer =“c”;
}
if
/ else if
语句更改为switch
语句。 else
语句
testString: assert(!/else/g.test(code));
- text: 您不应在编辑器中的任何位置使用任何if
语句
testString: assert(!/if/g.test(code));
- text: 你应该至少有四个break
语句
testString: assert(code.match(/break/g).length >= 4);
- text: chainToSwitch("bob")
应该是“Marley”
testString: assert(chainToSwitch("bob") === "Marley");
- text: chainToSwitch(42)
应该是“答案”
testString: assert(chainToSwitch(42) === "The Answer");
- text: chainToSwitch(1)
应该是“没有#1”
testString: "assert(chainToSwitch(1) === \"There is no #1\");"
- text: chainToSwitch(99)
应该是“错过了我这么多!”
testString: assert(chainToSwitch(99) === "Missed me by this much!");
- text: chainToSwitch(7)
应该是“Ate Nine”
testString: assert(chainToSwitch(7) === "Ate Nine");
- text: chainToSwitch("John")
应为“”(空字符串)
testString: assert(chainToSwitch("John") === "");
- text: chainToSwitch(156)
应为“”(空字符串)
testString: assert(chainToSwitch(156) === "");
```