freeCodeCamp/curriculum/challenges/spanish/02-javascript-algorithms-an.../basic-javascript/selecting-from-many-options...

3.1 KiB

id title challengeType videoUrl localeTitle
56533eb9ac21ba0edf2244dd Selecting from Many Options with Switch Statements 1 Selección de muchas opciones con instrucciones de cambio

Description

Si tiene muchas opciones para elegir, use una instrucción de 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.

Instructions

Escriba una instrucción de conmutación que pruebe val y establezca la answer para las siguientes condiciones:
1 - "alfa"
2 - "beta"
3 - "gamma"
4 - "delta"

Tests

tests:
  - text: <code>caseInSwitch(1)</code> debe tener un valor de &quot;alfa&quot;
    testString: 'assert(caseInSwitch(1) === "alpha", "<code>caseInSwitch(1)</code> should have a value of "alpha"");'
  - text: <code>caseInSwitch(2)</code> debe tener un valor de &quot;beta&quot;
    testString: 'assert(caseInSwitch(2) === "beta", "<code>caseInSwitch(2)</code> should have a value of "beta"");'
  - text: <code>caseInSwitch(3)</code> debe tener un valor de &quot;gamma&quot;
    testString: 'assert(caseInSwitch(3) === "gamma", "<code>caseInSwitch(3)</code> should have a value of "gamma"");'
  - text: <code>caseInSwitch(4)</code> debe tener un valor de &quot;delta&quot;
    testString: 'assert(caseInSwitch(4) === "delta", "<code>caseInSwitch(4)</code> should have a value of "delta"");'
  - text: No debes usar ninguna declaración <code>if</code> o <code>else</code>
    testString: 'assert(!/else/g.test(code) || !/if/g.test(code), "You should not use any <code>if</code> or <code>else</code> statements");'
  - text: Debe tener al menos 3 declaraciones de <code>break</code>
    testString: 'assert(code.match(/break/g).length > 2, "You should have at least 3 <code>break</code> statements");'

Challenge Seed

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



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

// Change this value to test
caseInSwitch(1);

Solution

// solution required