Repository navigation
Numbers #478
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
Open
diogoosz
wants to merge
3
commits into
javascript-tutorial:master
Choose a base branch
from
diogoosz:translate-numbers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Numbers #478
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
|
|
||
|
|
||
| ```js run demo | ||
| let a = +prompt("The first number?", ""); | ||
| let b = +prompt("The second number?", ""); | ||
| let a = +prompt("O primeiro número?", ""); | ||
| let b = +prompt("O segundo número?", ""); | ||
|
|
||
| alert( a + b ); | ||
| ``` | ||
|
|
||
| Note the unary plus `+` before `prompt`. It immediately converts the value to a number. | ||
| Observe o mais unário `+` antes de `prompt`. Ele converte o valor para um número imediatamente. | ||
|
|
||
| Otherwise, `a` and `b` would be string their sum would be their concatenation, that is: `"1" + "2" = "12"`. | ||
| Caso contrário, `a` e `b` seriam strings e a soma delas seria a concatenação, ou seja: `"1" + "2" = "12"`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 9 additions & 9 deletions
18
1-js/05-data-types/02-number/2-why-rounded-down/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,33 @@ | ||
| Internally the decimal fraction `6.35` is an endless binary. As always in such cases, it is stored with a precision loss. | ||
| Internamente, a fração decimal `6.35` é um binário infinito. Como sempre acontece nesses casos, ela é armazenada com perda de precisão. | ||
|
|
||
| Let's see: | ||
| Vamos ver: | ||
|
|
||
| ```js run | ||
| alert( 6.35.toFixed(20) ); // 6.34999999999999964473 | ||
| ``` | ||
|
|
||
| The precision loss can cause both increase and decrease of a number. In this particular case the number becomes a tiny bit less, that's why it rounded down. | ||
| A perda de precisão pode fazer um número tanto aumentar quanto diminuir. Nesse caso específico, o número fica um pouquinho menor, por isso ele foi arredondado para baixo. | ||
|
|
||
| And what's for `1.35`? | ||
| E quanto ao `1.35`? | ||
|
|
||
| ```js run | ||
| alert( 1.35.toFixed(20) ); // 1.35000000000000008882 | ||
| ``` | ||
|
|
||
| Here the precision loss made the number a little bit greater, so it rounded up. | ||
| Aqui a perda de precisão fez o número ficar um pouquinho maior, então ele foi arredondado para cima. | ||
|
|
||
| **How can we fix the problem with `6.35` if we want it to be rounded the right way?** | ||
| **Como podemos corrigir o problema com `6.35` se quisermos que ele seja arredondado da forma correta?** | ||
|
|
||
| We should bring it closer to an integer prior to rounding: | ||
| Devemos aproximá-lo de um número inteiro antes de arredondar: | ||
|
|
||
| ```js run | ||
| alert( (6.35 * 10).toFixed(20) ); // 63.50000000000000000000 | ||
| ``` | ||
|
|
||
| Note that `63.5` has no precision loss at all. That's because the decimal part `0.5` is actually `1/2`. Fractions divided by powers of `2` are exactly represented in the binary system, now we can round it: | ||
| Observe que `63.5` não tem nenhuma perda de precisão. Isso acontece porque a parte decimal `0.5` é, na verdade, `1/2`. Frações divididas por potências de `2` são representadas com exatidão no sistema binário, então agora podemos arredondar: | ||
|
|
||
|
|
||
| ```js run | ||
| alert( Math.round(6.35 * 10) / 10 ); // 6.35 -> 63.5 -> 64(rounded) -> 6.4 | ||
| alert( Math.round(6.35 * 10) / 10 ); // 6.35 -> 63.5 -> 64(arredondado) -> 6.4 | ||
| ``` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,29 +8,29 @@ afterEach(function() { | |
|
|
||
| describe("readNumber", function() { | ||
|
|
||
| it("if a number, returns it", function() { | ||
| it("se for um número, retorna ele", function() { | ||
|
Contributor
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. "retorna ele" (aqui e na linha 16) usa o pronome reto como objeto, o que é coloquial e foge da norma. Sugiro "retorna o número" / "retorna 0" (ou "retorna-o"). |
||
| prompt.returns("123"); | ||
| assert.strictEqual(readNumber(), 123); | ||
| }); | ||
|
|
||
| it("if 0, returns it", function() { | ||
| it("se for 0, retorna ele", function() { | ||
| prompt.returns("0"); | ||
| assert.strictEqual(readNumber(), 0); | ||
| }); | ||
|
|
||
| it("continues the loop until meets a number", function() { | ||
| prompt.onCall(0).returns("not a number"); | ||
| prompt.onCall(1).returns("not a number again"); | ||
| it("continua o laço até encontrar um número", function() { | ||
| prompt.onCall(0).returns("não é um número"); | ||
| prompt.onCall(1).returns("não é um número de novo"); | ||
| prompt.onCall(2).returns("1"); | ||
| assert.strictEqual(readNumber(), 1); | ||
| }); | ||
|
|
||
| it("if an empty line, returns null", function() { | ||
| it("se for uma linha vazia, retorna null", function() { | ||
| prompt.returns(""); | ||
| assert.isNull(readNumber()); | ||
| }); | ||
|
|
||
| it("if cancel, returns null", function() { | ||
| it("se for cancelado, retorna null", function() { | ||
| prompt.returns(null); | ||
| assert.isNull(readNumber()); | ||
| }); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
"mais unário" (aqui e nas linhas 193 e 388 do article.md) não bate com o termo do capítulo de operadores (
1-js/02-first-steps/08-operators/article.md:128,132: "+ unário" / "positivo unário"). Vale padronizar para que o leitor reconheça o mesmo conceito.