Skip to content

Commit 2f76d90

Browse files
committed
Merge branch 'master' of https://github.com/javascript-tutorial/pt.javascript.info into feature/steps-ifelse
2 parents f38a7d5 + b569358 commit 2f76d90

File tree

16 files changed

+307
-311
lines changed

16 files changed

+307
-311
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
To precisely match the functionality of `switch`, the `if` must use a strict comparison `'==='`.
1+
Para precisamente se equiparar à funcionalidade do `switch`, o `if` deve utilizar uma comparação exata `'==='`.
22

3-
For given strings though, a simple `'=='` works too.
3+
Contudo, para certas *strings*, um simples `'=='` também funciona.
44

55
```js no-beautify
66
if(browser == 'Edge') {
7-
alert("You've got the Edge!");
7+
alert("Você usa o Edge!");
88
} else if (browser == 'Chrome'
99
|| browser == 'Firefox'
1010
|| browser == 'Safari'
1111
|| browser == 'Opera') {
12-
alert( 'Okay we support these browsers too' );
12+
alert( 'Okay, também suportamos esse navegador.' );
1313
} else {
14-
alert( 'We hope that this page looks ok!' );
14+
alert( 'Esperamos que esta página tenha uma boa apresentação!' );
1515
}
1616
```
1717

18-
Please note: the construct `browser == 'Chrome' || browser == 'Firefox' …` is split into multiple lines for better readability.
18+
Por favor, note: a construção `browser == 'Chrome' || browser == 'Firefox' …` está repartida por múltiplas linhas para melhor leitura.
1919

20-
But the `switch` construct is still cleaner and more descriptive.
20+
Mas a construção `switch` ainda é mais clara e descritiva.

1-js/02-first-steps/14-switch/1-rewrite-switch-if-else/task.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ importance: 5
22

33
---
44

5-
# Rewrite the "switch" into an "if"
5+
# Transforme o "switch" num "if"
66

7-
Write the code using `if..else` which would correspond to the following `switch`:
7+
Escreva o código empregando `if..else` que corresponda ao seguinte `switch`:
88

99
```js
1010
switch (browser) {
1111
case 'Edge':
12-
alert( "You've got the Edge!" );
12+
alert( "Você usa o Edge!" );
1313
break;
1414

1515
case 'Chrome':
1616
case 'Firefox':
1717
case 'Safari':
1818
case 'Opera':
19-
alert( 'Okay we support these browsers too' );
19+
alert( 'Okay, também suportamos esse navegador' );
2020
break;
2121

2222
default:
23-
alert( 'We hope that this page looks ok!' );
23+
alert( 'Esperamos que esta página tenha uma boa apresentação!' );
2424
}
2525
```
2626

1-js/02-first-steps/14-switch/2-rewrite-if-switch/solution.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The first two checks turn into two `case`. The third check is split into two cases:
1+
As duas primeiras comparações transformam-se em dois `case`. A terceira comparação está divida em dois casos:
22

33
```js run
44
let a = +prompt('a?', '');
@@ -21,6 +21,6 @@ switch (a) {
2121
}
2222
```
2323

24-
Please note: the `break` at the bottom is not required. But we put it to make the code future-proof.
24+
Por favor, note: o `break` no final não é necessário. Mas o colocamos como segurança no código.
2525

26-
In the future, there is a chance that we'd want to add one more `case`, for example `case 4`. And if we forget to add a break before it, at the end of `case 3`, there will be an error. So that's a kind of self-insurance.
26+
No futuro, poderemos querer colocar mais um `case`, por exemplo `case 4`. E se nos esquecermos de adicionar um break antes dele, no final de `case 3`, ocorrerá um erro. Assim, é uma espécie de prevenção.

1-js/02-first-steps/14-switch/2-rewrite-if-switch/task.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 4
22

33
---
44

5-
# Rewrite "if" into "switch"
5+
# Transforme o "if" num "switch"
66

7-
Rewrite the code below using a single `switch` statement:
7+
Reescreva o código abaixo empregando uma única instrução `switch`:
88

99
```js run
1010
let a = +prompt('a?', '');
+57-57
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
# The "switch" statement
1+
# A instrução "switch"
22

3-
A `switch` statement can replace multiple `if` checks.
3+
Uma instrução `switch` pode substituir muitas comparações `if` (se).
44

5-
It gives a more descriptive way to compare a value with multiple variants.
5+
Ela fornece uma maneira mais descritiva de comparar um valor a múltiplas variantes.
66

7-
## The syntax
7+
## A sintaxe
88

9-
The `switch` has one or more `case` blocks and an optional default.
9+
O `switch` tem um ou mais blocos `case` (caso) e um `default` (padrão) opcional.
1010

11-
It looks like this:
11+
Ele se parece com:
1212

1313
```js no-beautify
1414
switch(x) {
15-
case 'value1': // if (x === 'value1')
15+
case 'valor1': // if (x === 'valor1')
1616
...
1717
[break]
1818

19-
case 'value2': // if (x === 'value2')
19+
case 'valor2': // if (x === 'valor2')
2020
...
2121
[break]
2222

@@ -26,71 +26,71 @@ switch(x) {
2626
}
2727
```
2828

29-
- The value of `x` is checked for a strict equality to the value from the first `case` (that is, `value1`) then to the second (`value2`) and so on.
30-
- If the equality is found, `switch` starts to execute the code starting from the corresponding `case`, until the nearest `break` (or until the end of `switch`).
31-
- If no case is matched then the `default` code is executed (if it exists).
29+
- O valor de `x` é comparado através de uma igualdade exata ao valor do primeiro `case` (isto é, ao `valor1`), a seguir ao do segundo (`valor2`), e assim sucessivamente.
30+
- Se uma igualdade for encontrada, o `switch` começa a executar o código a partir do início do `case` correspondente, até ao próximo `break` (ou até ao fim do `switch`).
31+
- Se nenhum `case` tem uma correspondência, então o código em `default` é executado (se existir).
3232

33-
## An example
33+
## Um exemplo
3434

35-
An example of `switch` (the executed code is highlighted):
35+
Um exemplo de `switch` (o código executado está em destaque):
3636

3737
```js run
3838
let a = 2 + 2;
3939

4040
switch (a) {
4141
case 3:
42-
alert( 'Too small' );
42+
alert( 'Muito baixo' );
4343
break;
4444
*!*
4545
case 4:
46-
alert( 'Exactly!' );
46+
alert( 'Exato!' );
4747
break;
4848
*/!*
4949
case 5:
50-
alert( 'Too big' );
50+
alert( 'Muito alto' );
5151
break;
5252
default:
53-
alert( "I don't know such values" );
53+
alert( "Não conheço tais valores" );
5454
}
5555
```
5656

57-
Here the `switch` starts to compare `a` from the first `case` variant that is `3`. The match fails.
57+
Aqui o `switch` começa por comparar `a` ao valor no primeiro `case`, isto é `3`. A correspondência falha.
5858

59-
Then `4`. That's a match, so the execution starts from `case 4` until the nearest `break`.
59+
A seguir a `4`. Existe uma correspondência, e assim a execução começa a partir do `case 4` até ao próximo `break`.
6060

61-
**If there is no `break` then the execution continues with the next `case` without any checks.**
61+
**Se não existir um `break` então a execução continua pelo próximo `case`, sem mais comparações.**
6262

63-
An example without `break`:
63+
Um exemplo sem `break`:
6464

6565
```js run
6666
let a = 2 + 2;
6767

6868
switch (a) {
6969
case 3:
70-
alert( 'Too small' );
70+
alert( 'Muito baixo' );
7171
*!*
7272
case 4:
73-
alert( 'Exactly!' );
73+
alert( 'Exato!' );
7474
case 5:
75-
alert( 'Too big' );
75+
alert( 'Muito alto' );
7676
default:
77-
alert( "I don't know such values" );
77+
alert( "Não conheço tais valores" );
7878
*/!*
7979
}
8080
```
8181

82-
In the example above we'll see sequential execution of three `alert`s:
82+
No exemplo acima, vemos uma execução sequential de três `alert`'s:
8383

8484
```js
85-
alert( 'Exactly!' );
86-
alert( 'Too big' );
87-
alert( "I don't know such values" );
85+
alert( 'Exato!' );
86+
alert( 'Muito alto' );
87+
alert( "Não conheço tais valores" );
8888
```
8989

90-
````smart header="Any expression can be a `switch/case` argument"
91-
Both `switch` and `case` allow arbitrary expressions.
90+
````smart header="Qualquer expressão pode servir de argumento a 'switch/case'"
91+
Ambos `switch` e `case` permitem expressões arbitrárias.
9292
93-
For example:
93+
Por exemplo:
9494
9595
```js run
9696
let a = "1";
@@ -99,74 +99,74 @@ let b = 0;
9999
switch (+a) {
100100
*!*
101101
case b + 1:
102-
alert("this runs, because +a is 1, exactly equals b+1");
102+
alert("isto é executado, porque +a é 1, e é exatamente igual a b+1");
103103
break;
104104
*/!*
105105
106106
default:
107-
alert("this doesn't run");
107+
alert("isto não é executado");
108108
}
109109
```
110-
Here `+a` gives `1`, that's compared with `b + 1` in `case`, and the corresponding code is executed.
110+
Aqui `+a` `1`, que é comparado no `case` a `b + 1`, e o código correspondente é executado.
111111
````
112112

113-
## Grouping of "case"
113+
## Grupos de "case"
114114

115-
Several variants of `case` which share the same code can be grouped.
115+
Múltiplas variantes de `case` que partilhem o mesmo código podem ser agrupadas.
116116

117-
For example, if we want the same code to run for `case 3` and `case 5`:
117+
Por exemplo, se quisermos que o mesmo código corra para `case 3` e `case 5`:
118118

119119
```js run no-beautify
120-
let a = 3;
120+
let a = 2 + 2;
121121

122122
switch (a) {
123123
case 4:
124-
alert('Right!');
124+
alert('Certo!');
125125
break;
126126

127127
*!*
128-
case 3: // (*) grouped two cases
128+
case 3: // (*) dois cases agrupados
129129
case 5:
130-
alert('Wrong!');
131-
alert("Why don't you take a math class?");
130+
alert('Errado!');
131+
alert("Porque não tem aulas de matemática?");
132132
break;
133133
*/!*
134134

135135
default:
136-
alert('The result is strange. Really.');
136+
alert('O resultado é estranho. Realmente.');
137137
}
138138
```
139139

140-
Now both `3` and `5` show the same message.
140+
Agora ambos `3` e `5` mostram a mesma mensagem.
141141

142-
The ability to "group" cases is a side effect of how `switch/case` works without `break`. Here the execution of `case 3` starts from the line `(*)` and goes through `case 5`, because there's no `break`.
142+
A habilidade para "agrupar" cases é um efeito secundário de como o `switch/case` funciona sem `break`. Aqui a execução do `case 3` começa pela linha `(*)` e prossegue pelo `case 5`, por não existir `break`.
143143

144-
## Type matters
144+
## O tipo de dados importa
145145

146-
Let's emphasize that the equality check is always strict. The values must be of the same type to match.
146+
Vamos enfatizar que a verificação da igualdade é sempre exata. Os valores devem também ser do mesmo tipo para existir correspondência.
147147

148-
For example, let's consider the code:
148+
Por exemplo, consideremos o código:
149149

150150
```js run
151-
let arg = prompt("Enter a value?");
151+
let arg = prompt("Insira um valor?");
152152
switch (arg) {
153153
case '0':
154154
case '1':
155-
alert( 'One or zero' );
155+
alert( 'Um ou zero' );
156156
break;
157157

158158
case '2':
159-
alert( 'Two' );
159+
alert( 'Dois' );
160160
break;
161161

162162
case 3:
163-
alert( 'Never executes!' );
163+
alert( 'Nunca executa!' );
164164
break;
165165
default:
166-
alert( 'An unknown value' );
166+
alert( 'Um valor desconhecido' );
167167
}
168168
```
169169

170-
1. For `0`, `1`, the first `alert` runs.
171-
2. For `2` the second `alert` runs.
172-
3. But for `3`, the result of the `prompt` is a string `"3"`, which is not strictly equal `===` to the number `3`. So we've got a dead code in `case 3`! The `default` variant will execute.
170+
1. Para `0`, `1`, o primeiro `alert` é executado.
171+
2. Para `2` o segundo `alert` corre.
172+
3. Mas para `3`, o resultado do `prompt` é uma string com o valor `"3"`, que não é estritamente igual `===` ao número `3`. Assim temos código ignorado em `case 3`! A variante `default` será executada.
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11

2-
Answers:
2+
Respostas:
33

44
1. `true`.
55

6-
The assignment to `Rabbit.prototype` sets up `[[Prototype]]` for new objects, but it does not affect the existing ones.
6+
A atribuição a `Rabbit.prototype` configura o `[[Prototype]]` para novos objetos, mas não afeta os objetos que já existem.
77

88
2. `false`.
99

10-
Objects are assigned by reference. The object from `Rabbit.prototype` is not duplicated, it's still a single object referenced both by `Rabbit.prototype` and by the `[[Prototype]]` of `rabbit`.
10+
Objetos são atribuídos por referência. O objeto do `Rabbit.prototype` não é duplicado, ele continua sendo um único objeto referenciado por `Rabbit.prototype` e pelo `[[Prototype]]` do `rabbit`.
1111

12-
So when we change its content through one reference, it is visible through the other one.
12+
Portanto, quando nós mudamos o seu conteúdo através de uma referência, ele fica visível para as outras.
1313

1414
3. `true`.
1515

16-
All `delete` operations are applied directly to the object. Here `delete rabbit.eats` tries to remove `eats` property from `rabbit`, but it doesn't have it. So the operation won't have any effect.
16+
Todas as operações de `delete` são aplicadas diretamente ao objeto. Neste caso, `delete rabbit.eats` tenta remover a propriedade `eats` do `rabbit`, mas ele não a tem. Assim, essa operação não tem nenhum efeito.
1717

1818
4. `undefined`.
1919

20-
The property `eats` is deleted from the prototype, it doesn't exist any more.
20+
A propriedade `eats` é deletada do protótipo, então ela realmente não existe mais.

1-js/08-prototypes/02-function-prototype/1-changing-prototype/task.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Changing "prototype"
5+
# Mudando o "prototype"
66

7-
In the code below we create `new Rabbit`, and then try to modify its prototype.
7+
No código abaixo, nós criamos um `new Rabbit`, depois tentamos modificar seu protótipo.
88

9-
In the start, we have this code:
9+
No começo, nós temos esse código:
1010

1111
```js run
1212
function Rabbit() {}
@@ -19,8 +19,7 @@ let rabbit = new Rabbit();
1919
alert( rabbit.eats ); // true
2020
```
2121

22-
23-
1. We added one more string (emphasized). What will `alert` show now?
22+
1. Nós adicionamos uma linha (realçada). O que o `alert` vai mostrar agora?
2423

2524
```js
2625
function Rabbit() {}
@@ -37,7 +36,7 @@ alert( rabbit.eats ); // true
3736
alert( rabbit.eats ); // ?
3837
```
3938

40-
2. ...And if the code is like this (replaced one line)?
39+
2. ...E se o código for assim (a linha foi substituída)?
4140

4241
```js
4342
function Rabbit() {}
@@ -54,7 +53,7 @@ alert( rabbit.eats ); // true
5453
alert( rabbit.eats ); // ?
5554
```
5655

57-
3. And like this (replaced one line)?
56+
3. E se for assim (a linha foi substituída)?
5857

5958
```js
6059
function Rabbit() {}
@@ -71,7 +70,7 @@ alert( rabbit.eats ); // true
7170
alert( rabbit.eats ); // ?
7271
```
7372

74-
4. The last variant:
73+
4. A última variação:
7574

7675
```js
7776
function Rabbit() {}

0 commit comments

Comments
 (0)