2.1 KiB
2.1 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
5690307fddb111c6084545d7 | Ordem Lógica em Instruções If Else | 1 | https://scrimba.com/c/cwNvMUV | 18228 | logical-order-in-if-else-statements |
--description--
Ordem é importante em instruções if
e else if
.
A função é executada de cima para baixo, então você deve ser cuidados com qual instrução vem primeiro.
Tomem essas duas funções como exemplo.
Aqui está a primeira:
function foo(x) {
if (x < 1) {
return "Less than one";
} else if (x < 2) {
return "Less than two";
} else {
return "Greater than or equal to two";
}
}
E a segunda apenas altera a ordem das instruções if e else if:
function bar(x) {
if (x < 2) {
return "Less than two";
} else if (x < 1) {
return "Less than one";
} else {
return "Greater than or equal to two";
}
}
Embora as duas funções se pareçam praticamente idênticas, se nós passarmos um número para ambos nós teremos saídas diferentes.
foo(0)
bar(0)
foo(0)
retornará a string Less than one
, e bar(0)
retornará a string Less than two
.
--instructions--
Altere a ordem lógica na função para que retorne as instruções corretas em todos os cenários.
--hints--
orderMyLogic(4)
deve retornar a string Less than 5
assert(orderMyLogic(4) === 'Less than 5');
orderMyLogic(6)
deve retornar a string Less than 10
assert(orderMyLogic(6) === 'Less than 10');
orderMyLogic(11)
deve retornar a string Greater than or equal to 10
assert(orderMyLogic(11) === 'Greater than or equal to 10');
--seed--
--seed-contents--
function orderMyLogic(val) {
if (val < 10) {
return "Less than 10";
} else if (val < 5) {
return "Less than 5";
} else {
return "Greater than or equal to 10";
}
}
orderMyLogic(7);
--solutions--
function orderMyLogic(val) {
if(val < 5) {
return "Less than 5";
} else if (val < 10) {
return "Less than 10";
} else {
return "Greater than or equal to 10";
}
}