2.3 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
| id | title | challengeType | videoUrl | forumTopicId | dashedName |
|---|---|---|---|---|---|
| 56533eb9ac21ba0edf2244de | Adicionar uma opção padrão em instruções switch | 1 | https://scrimba.com/c/c3JvVfg | 16653 | adding-a-default-option-in-switch-statements |
--description--
Na instrução switch, você não deve ser capaz de especificar todos os possíveis valores como instruções case. Ao invés disso, você pode adicionar a instrução default, que será executada se nenhuma instrução case correspondente for encontrada. Pense nisso como a instrução final else em uma cadeia de if/else.
A instrução default deve ser o último caso.
switch (num) {
case value1:
statement1;
break;
case value2:
statement2;
break;
...
default:
defaultStatement;
break;
}
--instructions--
Escreva a instrução switch para definir answer para as seguintes condições:
a - apple
b - bird
c - cat
default - stuff
--hints--
switchOfStuff("a") deve retornar a string apple
assert(switchOfStuff('a') === 'apple');
switchOfStuff("b") deve retornar a string bird
assert(switchOfStuff('b') === 'bird');
switchOfStuff("c") deve retornar a string cat
assert(switchOfStuff('c') === 'cat');
switchOfStuff("d") deve retornar a string stuff
assert(switchOfStuff('d') === 'stuff');
switchOfStuff(4) deve retornar a string stuff
assert(switchOfStuff(4) === 'stuff');
Você não deve usar nenhuma instrução do tipo if ou else
assert(!/else/g.test(code) || !/if/g.test(code));
Você deve usar a instrução default
assert(switchOfStuff('string-to-trigger-default-case') === 'stuff');
Você deve ter pelo menos 3 instruções break
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;
}