switch
puede ser más fácil de escribir que muchas instrucciones encadenadas if
/ else if
. El seguimiento: si (val === 1) {puede ser reemplazado con:
respuesta = "a";
} else if (val === 2) {
respuesta = "b";
} else {
respuesta = "c";
}
interruptor (val) {
caso 1:
respuesta = "a";
descanso;
caso 2:
respuesta = "b";
descanso;
defecto:
respuesta = "c";
}
if
/ else if
en una instrucción switch
. else
declaración en ningún lugar del editor.
testString: 'assert(!/else/g.test(code), "You should not use any else
statements anywhere in the editor");'
- text: No debe utilizar ninguna sentencia if
en ningún lugar del editor.
testString: 'assert(!/if/g.test(code), "You should not use any if
statements anywhere in the editor");'
- text: Debe tener al menos cuatro declaraciones de break
testString: 'assert(code.match(/break/g).length >= 4, "You should have at least four break
statements");'
- text: chainToSwitch("bob")
debe ser "Marley"
testString: 'assert(chainToSwitch("bob") === "Marley", "chainToSwitch("bob")
should be "Marley"");'
- text: chainToSwitch(42)
debe ser "La Respuesta"
testString: 'assert(chainToSwitch(42) === "The Answer", "chainToSwitch(42)
should be "The Answer"");'
- text: 'chainToSwitch(1)
debe ser "No hay # 1"'
testString: 'assert(chainToSwitch(1) === "There is no #1", "chainToSwitch(1)
should be "There is no #1"");'
- text: chainToSwitch(99)
debería ser "¡Me chainToSwitch(99)
por esto!"
testString: 'assert(chainToSwitch(99) === "Missed me by this much!", "chainToSwitch(99)
should be "Missed me by this much!"");'
- text: chainToSwitch(7)
debe ser "Ate Nine"
testString: 'assert(chainToSwitch(7) === "Ate Nine", "chainToSwitch(7)
should be "Ate Nine"");'
- text: chainToSwitch("John")
debe ser "" (cadena vacía)
testString: 'assert(chainToSwitch("John") === "", "chainToSwitch("John")
should be "" (empty string)");'
- text: chainToSwitch(156)
debe ser "" (cadena vacía)
testString: 'assert(chainToSwitch(156) === "", "chainToSwitch(156)
should be "" (empty string)");'
```