2021-06-15 00:49:18 -07:00
|
|
|
---
|
|
|
|
id: 5690307fddb111c6084545d7
|
2021-07-21 20:53:20 +05:30
|
|
|
title: Usar a ordem lógica em instruções if else
|
2021-06-15 00:49:18 -07:00
|
|
|
challengeType: 1
|
|
|
|
videoUrl: 'https://scrimba.com/c/cwNvMUV'
|
|
|
|
forumTopicId: 18228
|
|
|
|
dashedName: logical-order-in-if-else-statements
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
2021-07-30 01:41:44 +09:00
|
|
|
A ordem é importante em instruções `if` e `else if`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
2021-07-30 01:41:44 +09:00
|
|
|
A função é executada de cima para baixo, então você deve ser cuidadoso com qual instrução vem primeiro.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
2021-07-30 01:41:44 +09:00
|
|
|
Tomemos como exemplo estas duas funções.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
2021-07-14 21:02:51 +05:30
|
|
|
Aqui está a primeira:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-07-30 01:41:44 +09:00
|
|
|
A segunda apenas altera a ordem das instruções if e else if:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-07-30 01:41:44 +09:00
|
|
|
Embora as duas funções pareçam praticamente idênticas, se passarmos um número para ambas, teremos saídas diferentes.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
foo(0)
|
|
|
|
bar(0)
|
|
|
|
```
|
|
|
|
|
2021-07-30 01:41:44 +09:00
|
|
|
`foo(0)` retornará a string `Less than one` e `bar(0)` retornará a string `Less than two`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-07-14 21:02:51 +05:30
|
|
|
Altere a ordem lógica na função para que retorne as instruções corretas em todos os cenários.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-07-14 21:02:51 +05:30
|
|
|
`orderMyLogic(4)` deve retornar a string `Less than 5`
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(orderMyLogic(4) === 'Less than 5');
|
|
|
|
```
|
|
|
|
|
2021-07-14 21:02:51 +05:30
|
|
|
`orderMyLogic(6)` deve retornar a string `Less than 10`
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(orderMyLogic(6) === 'Less than 10');
|
|
|
|
```
|
|
|
|
|
2021-07-14 21:02:51 +05:30
|
|
|
`orderMyLogic(11)` deve retornar a string `Greater than or equal to 10`
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(orderMyLogic(11) === 'Greater than or equal to 10');
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
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--
|
|
|
|
|
|
|
|
```js
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|