4.1 KiB
4.1 KiB
id, title, localeTitle, challengeType
id | title | localeTitle | challengeType |
---|---|---|---|
56533eb9ac21ba0edf2244e0 | Replacing If Else Chains with Switch | Reemplazo de cadenas de otro tipo con interruptor | 1 |
Description
switch
puede ser más fácil de escribir que muchas instrucciones encadenadas if
/ else if
. Lo siguiente:
if (val === 1) {puede ser reemplazado por:
answer = "a";
} else if (val === 2) {
answer = "b";
} else {
answer = "c";
}
switch(val) {
case 1:
answer = "a";
break;
case 2:
answer = "b";
break;
default:
answer = "c";
}
Instructions
if
/ else if
encadenadas en una sentencia switch
.
Tests
tests:
- text: No debe utilizar ninguna <code>else</code> declaración en ningún lugar del editor.
testString: 'assert(!/else/g.test(code), "You should not use any <code>else</code> statements anywhere in the editor");'
- text: No debe utilizar ninguna sentencia <code>if</code> en ningún lugar del editor.
testString: 'assert(!/if/g.test(code), "You should not use any <code>if</code> statements anywhere in the editor");'
- text: Debe tener al menos cuatro declaraciones de <code>break</code>
testString: 'assert(code.match(/break/g).length >= 4, "You should have at least four <code>break</code> statements");'
- text: <code>chainToSwitch("bob")</code> debe ser "Marley"
testString: 'assert(chainToSwitch("bob") === "Marley", "<code>chainToSwitch("bob")</code> should be "Marley"");'
- text: <code>chainToSwitch(42)</code> debe ser "La Respuesta"
testString: 'assert(chainToSwitch(42) === "The Answer", "<code>chainToSwitch(42)</code> should be "The Answer"");'
- text: ' <code>chainToSwitch(1)</code> debe ser "No hay # 1"'
testString: 'assert(chainToSwitch(1) === "There is no #1", "<code>chainToSwitch(1)</code> should be "There is no #1"");'
- text: <code>chainToSwitch(99)</code> debería ser "¡Me <code>chainToSwitch(99)</code> por esto!"
testString: 'assert(chainToSwitch(99) === "Missed me by this much!", "<code>chainToSwitch(99)</code> should be "Missed me by this much!"");'
- text: <code>chainToSwitch(7)</code> debe ser "Ate Nine"
testString: 'assert(chainToSwitch(7) === "Ate Nine", "<code>chainToSwitch(7)</code> should be "Ate Nine"");'
- text: <code>chainToSwitch("John")</code> debe ser "" (cadena vacía)
testString: 'assert(chainToSwitch("John") === "", "<code>chainToSwitch("John")</code> should be "" (empty string)");'
- text: <code>chainToSwitch(156)</code> debe ser "" (cadena vacía)
testString: 'assert(chainToSwitch(156) === "", "<code>chainToSwitch(156)</code> should be "" (empty string)");'
Challenge Seed
function chainToSwitch(val) {
var answer = "";
// Only change code below this line
if (val === "bob") {
answer = "Marley";
} else if (val === 42) {
answer = "The Answer";
} else if (val === 1) {
answer = "There is no #1";
} else if (val === 99) {
answer = "Missed me by this much!";
} else if (val === 7) {
answer = "Ate Nine";
}
// Only change code above this line
return answer;
}
// Change this value to test
chainToSwitch(7);
Solution
function chainToSwitch(val) {
var answer = "";
switch(val) {
case "bob":
answer = "Marley";
break;
case 42:
answer = "The Answer";
break;
case 1:
answer = "There is no #1";
break;
case 99:
answer = "Missed me by this much!";
break;
case 7:
answer = "Ate Nine";
}
return answer;
}