switch
, você pode não conseguir especificar todos os valores possíveis como instruções case
. Em vez disso, você pode adicionar a instrução default
que será executada se nenhuma instrução case
correspondente for encontrada. Pense nisso como a final else
declaração em um if/else
cadeia. Uma declaração default
deve ser o último caso. switch (num) {
valor do caso1:
statement1;
pausa;
valor do caso2:
statement2;
pausa;
...
padrão:
defaultStatement;
pausa;
}
answer
para as seguintes condições: "a"
- "maçã" "b"
- "pássaro" "c"
- "gato" default
- "stuff" switchOfStuff("a")
deve ter um valor de "apple"
testString: 'assert(switchOfStuff("a") === "apple", "switchOfStuff("a")
should have a value of "apple"");'
- text: switchOfStuff("b")
deve ter um valor de "bird"
testString: 'assert(switchOfStuff("b") === "bird", "switchOfStuff("b")
should have a value of "bird"");'
- text: switchOfStuff("c")
deve ter um valor de "cat"
testString: 'assert(switchOfStuff("c") === "cat", "switchOfStuff("c")
should have a value of "cat"");'
- text: switchOfStuff("d")
deve ter um valor de "stuff"
testString: 'assert(switchOfStuff("d") === "stuff", "switchOfStuff("d")
should have a value of "stuff"");'
- text: switchOfStuff(4)
deve ter um valor de "stuff"
testString: 'assert(switchOfStuff(4) === "stuff", "switchOfStuff(4)
should have a value of "stuff"");'
- text: Você não deve usar nenhuma instrução if
ou else
testString: 'assert(!/else/g.test(code) || !/if/g.test(code), "You should not use any if
or else
statements");'
- text: Você deve usar uma declaração default
testString: 'assert(switchOfStuff("string-to-trigger-default-case") === "stuff", "You should use a default
statement");'
- text: Você deve ter pelo menos 3 declarações de break
testString: 'assert(code.match(/break/g).length > 2, "You should have at least 3 break
statements");'
```