-
Notifications
You must be signed in to change notification settings - Fork 110
Basic operators, math #247
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
Changes from all commits
161d378
ad5cdf0
4e7ed7f
b3c65d6
5d1c221
93812d7
b97f712
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 com uma string `"" + 1` converte `1` para uma string: `"" + 1 = "1"`, e então 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, esta converte uma string vazia `""` para `0`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
3. A adição com uma string anexa o número `5` à string. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 | ||||
---|---|---|---|---|---|---|
@@ -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 do `+`. Por exemplo, usando `Number()` ou precedendo-os com `+`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
For example, right before `prompt`: | ||||||
Por exemplo, logo antes de `prompt`: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```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 os `+` unário e binário no último código. Parece engraçado, não é mesmo? | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,17 +2,17 @@ importance: 5 | |||||
|
||||||
--- | ||||||
|
||||||
jonnathan-ls marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
# 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 valores padrões do prompt). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Why? Fix it. The result should be `3`. | ||||||
Por quê? Conserte isto. O resultado deve ser `3`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```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 | ||||||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.