2.3 KiB
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244de | Aggiungere un'opzione predefinita nelle dichiarazioni Switch | 1 | https://scrimba.com/c/c3JvVfg | 16653 | adding-a-default-option-in-switch-statements |
--description--
In un'istruzione switch
potresti non essere in grado di specificare tutti i possibili valori con le dichiarazioni case
. Puoi invece aggiungere una dichiarazione default
che verrà eseguita se non vengono trovati case
corrispondenti. Pensala come l'istruzione else
finale in una catena if/else
.
Un'istruzione default
dovrebbe essere l'ultimo caso.
switch (num) {
case value1:
statement1;
break;
case value2:
statement2;
break;
...
default:
defaultStatement;
break;
}
--instructions--
Scrivi un'istruzione switch per impostare answer
per le seguenti condizioni:
a
- apple
b
- bird
c
- cat
default
- stuff
--hints--
switchOfStuff("a")
dovrebbe restituire la stringa apple
assert(switchOfStuff('a') === 'apple');
switchOfStuff("b")
dovrebbe restituire la stringa bird
assert(switchOfStuff('b') === 'bird');
switchOfStuff("c")
dovrebbe restituire la stringa cat
assert(switchOfStuff('c') === 'cat');
switchOfStuff("d")
dovrebbe restituire la stringa stuff
assert(switchOfStuff('d') === 'stuff');
switchOfStuff(4)
dovrebbe restituire la stringa stuff
assert(switchOfStuff(4) === 'stuff');
Non dovresti usare alcuna dichiarazione if
o else
assert(!/else/g.test(code) || !/if/g.test(code));
Dovresti usare un'istruzione default
assert(switchOfStuff('string-to-trigger-default-case') === 'stuff');
Dovresti avere almeno altre 3 break
istruzioni
assert(code.match(/break/g).length > 2);
--seed--
--seed-contents--
function switchOfStuff(val) {
var answer = "";
// Only change code below this line
// Only change code above this line
return answer;
}
switchOfStuff(1);
--solutions--
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;
}