switch
pode ser mais fácil de gravar do que muitas instruções encadeadas if
/ else if
. Os seguintes: if (val === 1) {pode ser substituído por:
answer = "a";
} else if (val === 2) {
answer = "b";
} outro {
answer = "c";
}
interruptor (val) {
caso 1:
answer = "a";
pausa;
caso 2:
answer = "b";
pausa;
padrão:
answer = "c";
}
if
/ else if
para uma instrução switch
. else
instrução em nenhum lugar do editor
testString: 'assert(!/else/g.test(code), "You should not use any else
statements anywhere in the editor");'
- text: Você não deve usar nenhuma instrução if
nenhum lugar do editor
testString: 'assert(!/if/g.test(code), "You should not use any if
statements anywhere in the editor");'
- text: Você deve ter pelo menos quatro declarações de break
testString: 'assert(code.match(/break/g).length >= 4, "You should have at least four break
statements");'
- text: chainToSwitch("bob")
deve ser "Marley"
testString: 'assert(chainToSwitch("bob") === "Marley", "chainToSwitch("bob")
should be "Marley"");'
- text: chainToSwitch(42)
deve ser "A resposta"
testString: 'assert(chainToSwitch(42) === "The Answer", "chainToSwitch(42)
should be "The Answer"");'
- text: 'chainToSwitch(1)
deve ser "Não existe # 1"'
testString: 'assert(chainToSwitch(1) === "There is no #1", "chainToSwitch(1)
should be "There is no #1"");'
- text: chainToSwitch(99)
deve ser " chainToSwitch(99)
!"
testString: 'assert(chainToSwitch(99) === "Missed me by this much!", "chainToSwitch(99)
should be "Missed me by this much!"");'
- text: chainToSwitch(7)
deve ser "Ate Nine"
testString: 'assert(chainToSwitch(7) === "Ate Nine", "chainToSwitch(7)
should be "Ate Nine"");'
- text: chainToSwitch("John")
deve ser "" (string vazia)
testString: 'assert(chainToSwitch("John") === "", "chainToSwitch("John")
should be "" (empty string)");'
- text: chainToSwitch(156)
deve ser "" (string vazia)
testString: 'assert(chainToSwitch(156) === "", "chainToSwitch(156)
should be "" (empty string)");'
```