-
Notifications
You must be signed in to change notification settings - Fork 112
Loops: while and for #77
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
alvar-o
wants to merge
13
commits into
javascript-tutorial:master
Choose a base branch
from
alvar-o:master
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e4765a4
Translate while-for loops
f313154
deleting
b17dba1
Translate while-for loops
78ae1b7
corrections according to the review
alvar-o cfa3af4
fix typo
alvar-o 765fbe0
deleting all work to commit with proper credentials
alvar-o 787a0ec
commiting translation with proper credentials
alvar-o 1c87f6a
copying original files for reviewers to compare
alvar-o 0e8eaef
commiting translation with proper credentials
alvar-o b8d6da2
Translate a word.
odsantos 59c82e1
Correct a translation typo
odsantos 784e756
Remove duplicate translated word, suggest translation change, and rep…
odsantos e0fccfc
Remove duplicate translated word, suggest translation change, and rep…
odsantos 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
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
24 changes: 13 additions & 11 deletions
24
1-js/02-first-steps/12-while-for/2-which-value-while/solution.md
100644 → 100755
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,30 +1,32 @@ | ||
The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons. | ||
Este exercício demonstra como formas posfixadas/prefixadas podem levar a resultados distintos quando utilizadas em comparações. | ||
|
||
1. **From 1 to 4** | ||
1. **De 1 a 4** | ||
|
||
```js run | ||
let i = 0; | ||
while (++i < 5) alert( i ); | ||
``` | ||
|
||
The first value is `i = 1`, because `++i` first increments `i` and then returns the new value. So the first comparison is `1 < 5` and the `alert` shows `1`. | ||
O primeiro valor é `i = 1`, pois `++i` primeiro incrementa `i` e em seguida retorna o novo valor. Então, a primeira comparação é `1 < 5` e o `alert` exibe `1`. | ||
|
||
Then follow `2, 3, 4…` -- the values show up one after another. The comparison always uses the incremented value, because `++` is before the variable. | ||
Em seguida vêm `2, 3, 4…` -- os valores aparecem um depois do outro. A comparação sempre usa o valor incrementado, pois `++` vem antes da variável. | ||
|
||
Finally, `i = 4` is incremented to `5`, the comparison `while(5 < 5)` fails, and the loop stops. So `5` is not shown. | ||
2. **From 1 to 5** | ||
Finalmente, `i = 4` é incrementado a `5`, a comparação `while(5 < 5)` falha, e o loop termina. Assim, `5` não é exibido. | ||
|
||
2. **De 1 a 5** | ||
|
||
```js run | ||
let i = 0; | ||
while (i++ < 5) alert( i ); | ||
``` | ||
|
||
The first value is again `i = 1`. The postfix form of `i++` increments `i` and then returns the *old* value, so the comparison `i++ < 5` will use `i = 0` (contrary to `++i < 5`). | ||
O primeiro valor é novamente `i = 1`. A forma posfixada `i++`incrementa `i` e em seguida retorna o valor *antigo*, então a comparação `i++ < 5` utilizará `i = 0` (diferente de `++i < 5`). | ||
odsantos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
But the `alert` call is separate. It's another statement which executes after the increment and the comparison. So it gets the current `i = 1`. | ||
Mas a chamada de `alert` é separada. Ela é uma declaração distinta que executa após o incremento e a comparaçào. Assim, ela recebe o valor atual `i = 1`. | ||
|
||
Then follow `2, 3, 4…` | ||
Em seguida, vêm `2, 3, 4…` | ||
|
||
Let's stop on `i = 4`. The prefix form `++i` would increment it and use `5` in the comparison. But here we have the postfix form `i++`. So it increments `i` to `5`, but returns the old value. Hence the comparison is actually `while(4 < 5)` -- true, and the control goes on to `alert`. | ||
Vamos parar em `i = 4`. A forma prefixada `++i` iria incrementar e utilizar `5` na comparação. Mas aqui temos a forma posfixada `i++`. Ela incrementa `i` para `5`, mas retorna o valor antigo. Portanto a comparação é, na verdade, `while(4 < 5)` -- verdadeira, e o controle passa para `alert`. | ||
odsantos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false. | ||
O valor `i = 5` é o último, porque na próxima etapa `while(5 < 5)` é falso. | ||
|
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
14 changes: 7 additions & 7 deletions
14
1-js/02-first-steps/12-while-for/3-which-value-for/solution.md
100644 → 100755
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,17 +1,17 @@ | ||
**The answer: from `0` to `4` in both cases.** | ||
**Resposta: de `0` a `4` nos dois casos.** | ||
|
||
```js run | ||
for (let i = 0; i < 5; ++i) alert( i ); | ||
|
||
for (let i = 0; i < 5; i++) alert( i ); | ||
``` | ||
|
||
That can be easily deducted from the algorithm of `for`: | ||
Isso pode ser facilmente deduzido do algoritmo de `for`: | ||
|
||
1. Execute once `i = 0` before everything (begin). | ||
2. Check the condition `i < 5` | ||
3. If `true` -- execute the loop body `alert(i)`, and then `i++` | ||
1. Executar `i = 0` uma vez antes de tudo (início). | ||
2. Verificar a condição `i < 5`. | ||
odsantos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
3. Caso verdadeira (`true`), executar o corpo do loop `alert(i)`, e em seguida `i++`. | ||
odsantos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The increment `i++` is separated from the condition check (2). That's just another statement. | ||
O incremento `i++` é separado do teste da condição (2). Trata-se de outra declaração. | ||
|
||
The value returned by the increment is not used here, so there's no difference between `i++` and `++i`. | ||
O valor retornado pelo incremento não é utilizado aqui, então não há diferença entre `i++` e `++i`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
```js run | ||
let i = 0; | ||
while (i < 3) { | ||
alert( `number ${i}!` ); | ||
alert( `número ${i}!` ); | ||
i++; | ||
} | ||
``` | ||
|
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
24 changes: 12 additions & 12 deletions
24
1-js/02-first-steps/12-while-for/7-list-primes/solution.md
100644 → 100755
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,29 +1,29 @@ | ||
There are many algorithms for this task. | ||
Há muitos algotimos para esta tarefa. | ||
odsantos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Let's use a nested loop: | ||
Vamos utilizar um loop dentro de outro (isto é, aninhado): | ||
|
||
```js | ||
For each i in the interval { | ||
check if i has a divisor from 1..i | ||
if yes => the value is not a prime | ||
if no => the value is a prime, show it | ||
Para cada i no intervalo { | ||
testar se i tem um divisor de 1 a i | ||
se sim => o valor não é um primo | ||
se não => o valor e um primo, mostre-o | ||
} | ||
``` | ||
|
||
The code using a label: | ||
O código usando um label: | ||
|
||
```js run | ||
let n = 10; | ||
|
||
nextPrime: | ||
for (let i = 2; i <= n; i++) { // for each i... | ||
for (let i = 2; i <= n; i++) { // para cada i... | ||
|
||
for (let j = 2; j < i; j++) { // look for a divisor.. | ||
if (i % j == 0) continue nextPrime; // not a prime, go next i | ||
for (let j = 2; j < i; j++) { // procurar um divisor.. | ||
if (i % j == 0) continue nextPrime; // não é primo, passar para o próximo i | ||
} | ||
|
||
alert( i ); // a prime | ||
alert( i ); // é primo | ||
} | ||
``` | ||
|
||
There's a lot of space to opimize it. For instance, we could look for the divisors from `2` to square root of `i`. But anyway, if we want to be really efficient for large intervals, we need to change the approach and rely on advanced maths and complex algorithms like [Quadratic sieve](https://en.wikipedia.org/wiki/Quadratic_sieve), [General number field sieve](https://en.wikipedia.org/wiki/General_number_field_sieve) etc. | ||
Há muitas maneiras de otimizá-lo. Por exemplo, podemos procurar divisores de `2` até a raiz quadrada de `i`. De qualquer modo, se quisermos ser realmente eficientes para intervalos maiores, precisamos de mudar a abordagem e usar matemática avançada e algoritmos complexos como o [Crivo Quadrático](https://en.wikipedia.org/wiki/Quadratic_sieve), o [Campo de número de peneira geral](https://pt.wikipedia.org/wiki/Campo_de_n%C3%BAmero_de_peneira_geral), etc. |
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.
Uh oh!
There was an error while loading. Please reload this page.