switch
语句中,您可能无法将所有可能的值指定为case
语句。相反,您可以添加default
语句,如果找不到匹配的case
语句,将执行该语句。可以把它想象成if/else
链中的最后一个else
语句。 default
语句应该是最后一种情况。 switch(num){
案例值1:
语句1;
打破;
案例值2:
语句2;
打破;
...
默认:
defaultStatement;
打破;
}
answer
: "a"
- “苹果” "b"
- “鸟” "c"
- “猫” default
- “东西” switchOfStuff("a")
的值应为“apple”
testString: 'assert(switchOfStuff("a") === "apple", "switchOfStuff("a")
should have a value of "apple"");'
- text: switchOfStuff("b")
的值应为“bird”
testString: 'assert(switchOfStuff("b") === "bird", "switchOfStuff("b")
should have a value of "bird"");'
- text: switchOfStuff("c")
的值应为“cat”
testString: 'assert(switchOfStuff("c") === "cat", "switchOfStuff("c")
should have a value of "cat"");'
- text: switchOfStuff("d")
的值应为“stuff”
testString: 'assert(switchOfStuff("d") === "stuff", "switchOfStuff("d")
should have a value of "stuff"");'
- text: switchOfStuff(4)
的值应为“stuff”
testString: 'assert(switchOfStuff(4) === "stuff", "switchOfStuff(4)
should have a value of "stuff"");'
- text: 您不应该使用任何if
或else
语句
testString: 'assert(!/else/g.test(code) || !/if/g.test(code), "You should not use any if
or else
statements");'
- text: 您应该使用default
语句
testString: 'assert(switchOfStuff("string-to-trigger-default-case") === "stuff", "You should use a default
statement");'
- text: 你应该至少有3个break
语句
testString: 'assert(code.match(/break/g).length > 2, "You should have at least 3 break
statements");'
```