Skip to content

Basic operators, maths #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions 1-js/02-first-steps/08-operators/1-increment-order/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

The answer is:
A resposta é:

- `a = 2`
- `b = 2`
Expand All @@ -9,10 +9,10 @@ The answer is:
```js run no-beautify
let a = 1, b = 1;

alert( ++a ); // 2, prefix form returns the new value
alert( b++ ); // 1, postfix form returns the old value
alert( ++a ); // 2, a forma prefixa retorna o novo valor
alert( b++ ); // 1, a forma pós-fixa retorna o valor antigo

alert( a ); // 2, incremented once
alert( b ); // 2, incremented once
alert( a ); // 2, incrementado uma vez
alert( b ); // 2, incrementado uma vez
```

4 changes: 2 additions & 2 deletions 1-js/02-first-steps/08-operators/1-increment-order/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# The postfix and prefix forms
# Formas pós-fixa e prefixa

What are the final values of all variables `a`, `b`, `c` and `d` after the code below?
Quais são os valores finais de todas as variáveis `a`, `b`, `c` e `d` após o código abaixo?

```js
let a = 1, b = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The answer is:
A resposta é:

- `a = 4` (multiplied by 2)
- `x = 5` (calculated as 1 + 4)
- `a = 4` (multiplicado por 2)
- `x = 5` (calculado como 1 + 4)

4 changes: 2 additions & 2 deletions 1-js/02-first-steps/08-operators/2-assignment-result/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# Assignment result
# Resultado da atribuição

What are the values of `a` and `x` after the code below?
Quais são os valores de `a` e `x` após o código abaixo?

```js
let a = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ undefined + 1 = NaN // (6)
" \t \n" - 2 = -2 // (7)
```

1. The addition with a string `"" + 1` converts `1` to a string: `"" + 1 = "1"`, and then we have `"1" + 0`, the same rule is applied.
2. The subtraction `-` (like most math operations) only works with numbers, it converts an empty string `""` to `0`.
3. The addition with a string appends the number `5` to the string.
4. The subtraction always converts to numbers, so it makes `" -9 "` a number `-9` (ignoring spaces around it).
5. `null` becomes `0` after the numeric conversion.
6. `undefined` becomes `NaN` after the numeric conversion.
7. Space characters are trimmed off string start and end when a string is converted to a number. Here the whole string consists of space characters, such as `\t`, `\n` and a "regular" space between them. So, similarly to an empty string, it becomes `0`.
1. A adição de uma string `"" + 1` converte `1` para uma string: `"" + 1 = "1"`, e quando nós temos `"1" + 0`, a mesma regra é aplicada.
2. A subtração `-` (como a maioria das operações matemáticas) apenas funciona com números, e converte uma string vazia `""` para `0`.
3. A adição a uma string anexa o número `5` à string.
4. A subtração sempre converte para números, de modo que esta transforma `" -9 "` no número `-9` (ignorando os espaços em volta deste).
5. `null` se torna `0` após a conversão numérica.
6. `undefined` se torna `NaN` após a conversão numérica.
7. Caracteres de espaço, são aparados do início e final de uma string quando esta é convertida em um número. Aqui a string inteira consiste em caracteres de espaço, tais como `\t`, `\n` e um espaço "regular" entre eles. Então, similarmente à string vazia, isto se torna `0`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Type conversions
# Conversões de tipo

What are results of these expressions?
Quais são os resultados destas expressões?

```js no-beautify
"" + 1 + 0
Expand All @@ -23,4 +23,4 @@ undefined + 1
" \t \n" - 2
```

Think well, write down and then compare with the answer.
Pense bem, escreva e então compare com a resposta.
24 changes: 12 additions & 12 deletions 1-js/02-first-steps/08-operators/4-fix-prompt/solution.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
The reason is that prompt returns user input as a string.
A razão é que o prompt retorna o input do usuário como uma string.

So variables have values `"1"` and `"2"` respectively.
Então as variáveis têm valores `"1"` e `"2"`, respectivamente.

```js run
let a = "1"; // prompt("First number?", 1);
let b = "2"; // prompt("Second number?", 2);
let a = "1"; // prompt("Primeiro número?", 1);
let b = "2"; // prompt("Segundo número?", 2);

alert(a + b); // 12
```

What we should do is to convert strings to numbers before `+`. For example, using `Number()` or prepending them with `+`.
O que nós devemos fazer é converter strings em números antes da adição (`+`). Por exemplo, usando `Number()` ou precedendo-os com `+`.

For example, right before `prompt`:
Por exemplo, logo antes do `prompt`:

```js run
let a = +prompt("First number?", 1);
let b = +prompt("Second number?", 2);
let a = +prompt("Primeiro número?", 1);
let b = +prompt("Segundo número?", 2);

alert(a + b); // 3
```

Or in the `alert`:
Ou no `alert`:

```js run
let a = prompt("First number?", 1);
let b = prompt("Second number?", 2);
let a = prompt("Primeiro número?", 1);
let b = prompt("Segundo número?", 2);

alert(+a + +b); // 3
```

Using both unary and binary `+` in the latest code. Looks funny, doesn't it?
Usando ambos `+` unário e binário no último código. Parece engraçado, não é mesmo?
12 changes: 6 additions & 6 deletions 1-js/02-first-steps/08-operators/4-fix-prompt/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Fix the addition
# Conserte a adição

Here's a code that asks the user for two numbers and shows their sum.
Aqui está um código que pede dois números ao usuário e mostra a soma dos mesmos.

It works incorrectly. The output in the example below is `12` (for default prompt values).
Ele funciona incorretamente. A saída no exemplo abaixo é `12` (para os valores presentes por padrão no prompt, definidos pelo segundo argumento).

Why? Fix it. The result should be `3`.
Por quê? Conserte isto. O resultado deveria ser `3`.

```js run
let a = prompt("First number?", 1);
let b = prompt("Second number?", 2);
let a = prompt("Primeiro número?", 1);
let b = prompt("Segundo número?", 2);

alert(a + b); // 12
```
Loading