3.1 KiB
3.1 KiB
id, title, challengeType, guideUrl, videoUrl, localeTitle
id | title | challengeType | guideUrl | videoUrl | localeTitle |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244de | Adding a Default Option in Switch Statements | 1 | https://chinese.freecodecamp.org/guide/certificates/adding-a-default-option-in-switch-statements | 在交换机语句中添加默认选项 |
Description
switch
语句中,您可能无法将所有可能的值指定为case
语句。相反,您可以添加default
语句,如果找不到匹配的case
语句,将执行该语句。可以把它想象成if/else
链中的最后一个else
语句。 default
语句应该是最后一种情况。 switch(num){
案例值1:
语句1;
打破;
案例值2:
语句2;
打破;
...
默认:
defaultStatement;
打破;
}
Instructions
answer
: "a"
- “苹果” "b"
- “鸟” "c"
- “猫” default
- “东西” Tests
tests:
- text: <code>switchOfStuff("a")</code>的值应为“apple”
testString: 'assert(switchOfStuff("a") === "apple", "<code>switchOfStuff("a")</code> should have a value of "apple"");'
- text: <code>switchOfStuff("b")</code>的值应为“bird”
testString: 'assert(switchOfStuff("b") === "bird", "<code>switchOfStuff("b")</code> should have a value of "bird"");'
- text: <code>switchOfStuff("c")</code>的值应为“cat”
testString: 'assert(switchOfStuff("c") === "cat", "<code>switchOfStuff("c")</code> should have a value of "cat"");'
- text: <code>switchOfStuff("d")</code>的值应为“stuff”
testString: 'assert(switchOfStuff("d") === "stuff", "<code>switchOfStuff("d")</code> should have a value of "stuff"");'
- text: <code>switchOfStuff(4)</code>的值应为“stuff”
testString: 'assert(switchOfStuff(4) === "stuff", "<code>switchOfStuff(4)</code> should have a value of "stuff"");'
- text: 您不应该使用任何<code>if</code>或<code>else</code>语句
testString: 'assert(!/else/g.test(code) || !/if/g.test(code), "You should not use any <code>if</code> or <code>else</code> statements");'
- text: 您应该使用<code>default</code>语句
testString: 'assert(switchOfStuff("string-to-trigger-default-case") === "stuff", "You should use a <code>default</code> statement");'
- text: 你应该至少有3个<code>break</code>语句
testString: 'assert(code.match(/break/g).length > 2, "You should have at least 3 <code>break</code> statements");'
Challenge Seed
function switchOfStuff(val) {
var answer = "";
// Only change code below this line
// Only change code above this line
return answer;
}
// Change this value to test
switchOfStuff(1);
Solution
// solution required