3.8 KiB
3.8 KiB
id, title, localeTitle, challengeType, guideUrl
id | title | localeTitle | challengeType | guideUrl |
---|---|---|---|---|
56533eb9ac21ba0edf2244de | Adding a Default Option in Switch Statements | Adición de una opción predeterminada en los estados de cambio | 1 | https://spanish.freecodecamp.org/guide/certificates/adding-a-default-option-in-switch-statements |
Description
switch
, es posible que no pueda especificar todos los valores posibles como declaraciones de case
. En su lugar, puede agregar la declaración default
que se ejecutará si no se encuentran declaraciones de case
coincidentes. Piense en ello como la última instrucción else
en una cadena if/else
.
Una declaración por default
debe ser el último caso.
switch (num) {
case value1:
statement1;
break;
case value2:
statement2;
break;
...
default:
defaultStatement;
break;
}
Instructions
answer
para las siguientes condiciones: "a"
- "manzana" "b"
- "pájaro" "c"
- "gato" default
- "cosas"
Tests
tests:
- text: <code>switchOfStuff("a")</code> debe tener un valor de "apple"
testString: 'assert(switchOfStuff("a") === "apple", "<code>switchOfStuff("a")</code> should have a value of "apple"");'
- text: <code>switchOfStuff("b")</code> debe tener un valor de "bird"
testString: 'assert(switchOfStuff("b") === "bird", "<code>switchOfStuff("b")</code> should have a value of "bird"");'
- text: <code>switchOfStuff("c")</code> debe tener un valor de "cat"
testString: 'assert(switchOfStuff("c") === "cat", "<code>switchOfStuff("c")</code> should have a value of "cat"");'
- text: <code>switchOfStuff("d")</code> debe tener un valor de "cosas"
testString: 'assert(switchOfStuff("d") === "stuff", "<code>switchOfStuff("d")</code> should have a value of "stuff"");'
- text: <code>switchOfStuff(4)</code> debe tener un valor de "cosas"
testString: 'assert(switchOfStuff(4) === "stuff", "<code>switchOfStuff(4)</code> should have a value of "stuff"");'
- 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: Debes usar una declaración por <code>default</code>
testString: 'assert(switchOfStuff("string-to-trigger-default-case") === "stuff", "You should use a <code>default</code> statement");'
- 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 switchOfStuff(val) {
var answer = "";
// Only change code below this line
// Only change code above this line
return answer;
}
// Change this value to test
switchOfStuff(1);
Solution
function switchOfStuff(val) {
var answer = "";
switch(val) {
case "a":
answer = "apple";
break;
case "b":
answer = "bird";
break;
case "c":
answer = "cat";
break;
default:
answer = "stuff";
}
return answer;
}