2.1 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244de 在 Switch 语句中添加默认选项 1 https://scrimba.com/c/c3JvVfg 16653 adding-a-default-option-in-switch-statements

--description--

switch 语句中你可能无法用 case 来指明所有情况,这时我们可以添加 default 语句来解决这个问题。在无法找到匹配的 case 时default 语句就会执行。这非常类似于 if/else 语句中的 else 语句。

注意,default 语句应该在所有的 case 之后。

switch (num) {
  case value1:
    statement1;
    break;
  case value2:
    statement2;
    break;
...
  default:
    defaultStatement;
    break;
}

--instructions--

请实现根据下面的条件来设置 answerswitch 语句:
"a" - "apple"
"b" - "bird"
"c" - "cat"
default - "stuff"

--hints--

switchOfStuff("a") 的值应为 "apple"。

assert(switchOfStuff('a') === 'apple');

switchOfStuff("b") 的值应为 "bird"。

assert(switchOfStuff('b') === 'bird');

switchOfStuff("c") 的值应为 "cat"。

assert(switchOfStuff('c') === 'cat');

switchOfStuff("d") 的值应为 "stuff"。

assert(switchOfStuff('d') === 'stuff');

switchOfStuff("4") 的值应为 "stuff"。

assert(switchOfStuff(4) === 'stuff');

不应使用 ifelse 语句。

assert(!/else/g.test(code) || !/if/g.test(code));

应有一个 default 语句。

assert(switchOfStuff('string-to-trigger-default-case') === 'stuff');

应至少有 3 个 break 语句。

assert(code.match(/break/g).length > 2);

--seed--

--seed-contents--

function switchOfStuff(val) {
  var answer = "";
  // Only change code below this line



  // Only change code above this line
  return answer;
}

switchOfStuff(1);

--solutions--

function switchOfStuff(val) {
  var answer = "";

  switch(val) {
    case "a":
      answer = "apple";
      break;
    case "b":
      answer = "bird";
      break;
    case "c":
      answer = "cat";
      break;
    default:
      answer = "stuff";
  }
  return answer;
}