2021-06-15 07:49:18 +00:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244dc
|
2021-07-10 04:23:54 +00:00
|
|
|
title: Encadeando Instruções If Else
|
2021-06-15 07:49:18 +00:00
|
|
|
challengeType: 1
|
|
|
|
videoUrl: 'https://scrimba.com/c/caeJgsw'
|
|
|
|
forumTopicId: 16772
|
|
|
|
dashedName: chaining-if-else-statements
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`if/else` instruções podem ser encadeadas por uma lógica complexa. Aqui está o <dfn>pseudocódigo</dfn> de várias instruções encadeadas `if` / `else`:
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
if (condition1) {
|
|
|
|
statement1
|
|
|
|
} else if (condition2) {
|
|
|
|
statement2
|
|
|
|
} else if (condition3) {
|
|
|
|
statement3
|
|
|
|
. . .
|
|
|
|
} else {
|
|
|
|
statementN
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
Escreva instruções encadeadas `if`/`else` para atender as seguintes condições:
|
2021-06-15 07:49:18 +00:00
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`num < 5` - retorna `Tiny`
|
|
|
|
`num < 10` - retorna `Small`
|
|
|
|
`num < 15` - retorna `Medium`
|
|
|
|
`num < 20` - retorna `Large`
|
|
|
|
`num >= 20` - retorna `Huge`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
Você deve ter pelo menos quatro instruções `else`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(code.match(/else/g).length > 3);
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
Você deve ter pelo menos quatro instruções `if`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(code.match(/if/g).length > 3);
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
Você deve ter pelo menos um comando `return`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(code.match(/return/g).length >= 1);
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`testSize(0)` deve retornar a string `Tiny`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(testSize(0) === 'Tiny');
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`testSize(4)` deve retornar a string `Tiny`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(testSize(4) === 'Tiny');
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`testSize(5)` deve retornar a string `Small`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(testSize(5) === 'Small');
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`testSize(8)` deve retornar a string `Small`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(testSize(8) === 'Small');
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`testSize(10)` deve retornar a string `Medium`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(testSize(10) === 'Medium');
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`testSize(14)` deve retornar a string `Medium`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(testSize(14) === 'Medium');
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`testSize(15)` deve retornar a string `Large`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(testSize(15) === 'Large');
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`testSize(17)` deve retornar a string `Large`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(testSize(17) === 'Large');
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`testSize(20)` deve retornar a string `Huge`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(testSize(20) === 'Huge');
|
|
|
|
```
|
|
|
|
|
2021-07-10 04:23:54 +00:00
|
|
|
`testSize(25)` deve retornar a string `Huge`
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(testSize(25) === 'Huge');
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function testSize(num) {
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
|
|
return "Change Me";
|
|
|
|
// Only change code above this line
|
|
|
|
}
|
|
|
|
|
|
|
|
testSize(7);
|
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function testSize(num) {
|
|
|
|
if (num < 5) {
|
|
|
|
return "Tiny";
|
|
|
|
} else if (num < 10) {
|
|
|
|
return "Small";
|
|
|
|
} else if (num < 15) {
|
|
|
|
return "Medium";
|
|
|
|
} else if (num < 20) {
|
|
|
|
return "Large";
|
|
|
|
} else {
|
|
|
|
return "Huge";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|