switch
. Una instrucción de switch
prueba un valor y puede tener muchas declaraciones de case
que definen varios valores posibles. Las declaraciones se ejecutan desde el primer valor de case
coincidente hasta que se encuentra una break
. Aquí hay un ejemplo de pseudocódigo : interruptor (núm) {
valor de caso1:
sentencia1;
descanso;
valor de caso2:
declaración2;
descanso;
...
valor de casoN:
declaración N;
descanso;
}
case
valores de case
se prueban con igualdad estricta ( ===
). La break
le dice a JavaScript que deje de ejecutar sentencias. Si se omite la break
, se ejecutará la siguiente instrucción. val
y establezca la answer
para las siguientes condiciones: 1
- "alfa" 2
- "beta" 3
- "gamma" 4
- "delta" caseInSwitch(1)
debe tener un valor de "alfa"
testString: 'assert(caseInSwitch(1) === "alpha", "caseInSwitch(1)
should have a value of "alpha"");'
- text: caseInSwitch(2)
debe tener un valor de "beta"
testString: 'assert(caseInSwitch(2) === "beta", "caseInSwitch(2)
should have a value of "beta"");'
- text: caseInSwitch(3)
debe tener un valor de "gamma"
testString: 'assert(caseInSwitch(3) === "gamma", "caseInSwitch(3)
should have a value of "gamma"");'
- text: caseInSwitch(4)
debe tener un valor de "delta"
testString: 'assert(caseInSwitch(4) === "delta", "caseInSwitch(4)
should have a value of "delta"");'
- text: No debes usar ninguna declaración if
o else
testString: 'assert(!/else/g.test(code) || !/if/g.test(code), "You should not use any if
or else
statements");'
- text: Debe tener al menos 3 declaraciones de break
testString: 'assert(code.match(/break/g).length > 2, "You should have at least 3 break
statements");'
```