Loops: while and for #171
新建议题
Have a question about this project? 注册 for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “注册 for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? 登录 to your account
Open
andreimuntean1
wants to merge
9
commits into
javascript-tutorial:master
Choose a base branch
from
andreimuntean1:while-for
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
Loops: while and for #171
Changes from all commits
提交
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9da651d
Translated article.md
andreimuntean1 1afb63a
Translated solution.md
andreimuntean1 a70f72a
Translated task.md
andreimuntean1 d8f4e09
Translate article.md
andreimuntean1 4a59e05
Translated tasks
andreimuntean1 7ef3d55
Revert "Translated task.md"
andreimuntean1 c572b8c
Revert "Translated solution.md"
andreimuntean1 4b7f043
Revert "Translated article.md"
andreimuntean1 bb5bfcc
Made required changes
andreimuntean1 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
40 changes: 17 additions & 23 deletions
40
1-js/02-first-steps/13-while-for/2-which-value-while/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,30 +1,24 @@ | ||
| The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons. | ||
| Acest exercițiu demonstrează cum prefixele/sufixele pot duce la rezultate diferite când sunt comparate. | ||
|
|
||
| 1. **From 1 to 4** | ||
| 1. **De la 1 la 4** | ||
|
|
||
| ```js run | ||
| let i = 0; | ||
| while (++i < 5) alert( i ); | ||
| ``` | ||
| ```js run | ||
| let i = 0; | ||
| while (++i < 5) alert( i ); | ||
| ``` | ||
| Prima valoare este `i = 1` pentru că `++i` incrementează prima dată `i` și apoi returnează noua valoare. Așadar prima comparație este `1 < 5` și funcția `alert` afișează `1`. | ||
| 2. **De la 1 la 5** | ||
|
|
||
| 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`. | ||
| ```js run | ||
| let i = 0; | ||
| while (i++ < 5) alert( i ); | ||
| ``` | ||
| Prima valoare este din nou `i = 1`. Forma cu sufix `i++` incrementează `i` și după returnează vechea valoare, deci comparația `i++ < 5` va folosi `i = 0` (față de `++i < 5`). | ||
|
|
||
| Then follow `2, 3, 4…` -- the values show up one after another. The comparison always uses the incremented value, because `++` is before the variable. | ||
| Dar funcția `alert` se apelează separat. Este altă linie de cod care se execută după incrementare și după comparație. Așadar primește `i = 0`. | ||
|
|
||
| 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** | ||
| Apoi urmând `2, 3, 4...` | ||
|
|
||
| ```js run | ||
| let i = 0; | ||
| while (i++ < 5) alert( i ); | ||
| ``` | ||
| Hai să ne oprim la `i = 4`. Forma cu prefix `++i` l-ar incrementa și am folosi `5` pentru comparație. Dar aici avem forma cu sufix `i++`. Astfel îl incrementează pe `i` la `5`, dar returnează vechea valoare. Datorită faptului că comparația e de fapt `while(4 < 5)` - adevărat și execuția codului continuă cu funcția `alert`. | ||
|
|
||
| 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`). | ||
|
|
||
| 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`. | ||
|
|
||
| Then follow `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`. | ||
|
|
||
| The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false. | ||
| Valoarea `i = 5` esre ultima, deoarece următorul pas `while(5 < 5)` este fals. | ||
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 |
|---|---|---|
|
|
@@ -2,21 +2,21 @@ importance: 4 | |
|
|
||
| --- | ||
|
|
||
| # Which values does the while loop show? | ||
| # Ce valori afișează bucla while? | ||
|
|
||
| For every loop iteration, write down which value it outputs and then compare it with the solution. | ||
| Pentru fiecare iterație a buclei, scrie ce valori afișează și apoi compară-le cu soluția. | ||
|
|
||
| Both loops `alert` the same values, or not? | ||
| Ambele bucle afișează prin funcția `alert` aceleași valori, sau nu? | ||
|
|
||
| 1. The prefix form `++i`: | ||
| 1. Forma cu prefix `++i`: | ||
|
|
||
| ```js | ||
|
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. formatare |
||
| let i = 0; | ||
| while (++i < 5) alert( i ); | ||
| ``` | ||
| 2. The postfix form `i++` | ||
| ```js | ||
| let i = 0; | ||
| while (++i < 5) alert( i ); | ||
| ``` | ||
| 2. Forma cu sufix `i++` | ||
|
|
||
| ```js | ||
| let i = 0; | ||
| while (i++ < 5) alert( i ); | ||
| ``` | ||
| ```js | ||
| let i = 0; | ||
| while (i++ < 5) alert( i ); | ||
| ``` | ||
14 changes: 7 additions & 7 deletions
14
1-js/02-first-steps/13-while-for/3-which-value-for/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,17 +1,17 @@ | ||
| **The answer: from `0` to `4` in both cases.** | ||
| **Răspunsul: de la 0 la 4 în ambele cazuri.** | ||
|
|
||
| ```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`: | ||
| Asta se poate deduce ușor din algoritmul lui `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. Execută o dată `i = 0` înainte a orice altceva (inițializator). | ||
| 2. Verifică condiția `i < 5` | ||
| 3. Dacă e `true` -- rulează corpul buclei `alert(i)`, și apoi `i++` | ||
|
|
||
| The increment `i++` is separated from the condition check (2). That's just another statement. | ||
| Incrementarea `i++` e separată de verificarea condiției (2). Aceea e pur și simplu altă bucată de cod. | ||
|
|
||
| The value returned by the increment is not used here, so there's no difference between `i++` and `++i`. | ||
| Valoarea returnată după incrementare nu e folosită aici, așadar nu este nici-o diferență între `i++` ș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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,19 +2,19 @@ importance: 4 | |
|
|
||
| --- | ||
|
|
||
| # Which values get shown by the "for" loop? | ||
| # Ce valori se afișează pentru bucla "for"? | ||
|
|
||
| For each loop write down which values it is going to show. Then compare with the answer. | ||
| Pentru fiecare buclă, scrie ce valori se vor afișa. Apoi compară-le cu soluția. | ||
|
|
||
| Both loops `alert` same values or not? | ||
| Ambele bucle afișează prin funcția `alert` aceleași valori sau nu? | ||
|
|
||
| 1. The postfix form: | ||
| 1. Forma cu sufix: | ||
|
|
||
| ```js | ||
|
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. formatare |
||
| for (let i = 0; i < 5; i++) alert( i ); | ||
| ``` | ||
| 2. The prefix form: | ||
| ```js | ||
| for (let i = 0; i < 5; i++) alert( i ); | ||
| ``` | ||
| 2. Forma cu prefix: | ||
|
|
||
| ```js | ||
| for (let i = 0; i < 5; ++i) alert( i ); | ||
| ``` | ||
| ```js | ||
| for (let i = 0; i < 5; ++i) alert( 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
|
|
||
|
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. linii șterse |
||
|
|
||
| ```js run demo | ||
| for (let i = 2; i <= 10; i++) { | ||
| if (i % 2 == 0) { | ||
|
|
@@ -8,4 +6,4 @@ for (let i = 2; i <= 10; i++) { | |
| } | ||
| ``` | ||
|
|
||
| We use the "modulo" operator `%` to get the remainder and check for the evenness here. | ||
| Folosim operatorul "modulo" `%` pentru a afla restul împărțirii și astfel a verifica paritatea. | ||
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
5 changes: 1 addition & 4 deletions
5
1-js/02-first-steps/13-while-for/5-replace-for-while/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,10 +1,7 @@ | ||
|
|
||
|
|
||
| ```js run | ||
| let i = 0; | ||
| while (i < 3) { | ||
| alert( `number ${i}!` ); | ||
| alert( `numărul ${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
11 changes: 5 additions & 6 deletions
11
1-js/02-first-steps/13-while-for/6-repeat-until-correct/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,15 +1,14 @@ | ||
|
|
||
| ```js run demo | ||
| let num; | ||
|
|
||
| do { | ||
| num = prompt("Enter a number greater than 100?", 0); | ||
| num = prompt("Introdu un număr mai mare ca 100?", 0); | ||
| } while (num <= 100 && num); | ||
| ``` | ||
|
|
||
| The loop `do..while` repeats while both checks are truthy: | ||
| Bucla `do..while` se repetă până amândouă verificările sunt truthy: | ||
|
|
||
| 1. The check for `num <= 100` -- that is, the entered value is still not greater than `100`. | ||
| 2. The check `&& num` is false when `num` is `null` or an empty string. Then the `while` loop stops too. | ||
| 1. Verificarea ca `num <= 100` -- adică, valoarea introdusă încă nu e mai mare ca `100` | ||
| 2. Verificarea `&& num` e falsă când `num` e fie `null` fie un string gol. Atunci bucla `while` se oprește și ea. | ||
|
|
||
| P.S. If `num` is `null` then `num <= 100` is `true`, so without the 2nd check the loop wouldn't stop if the user clicks CANCEL. Both checks are required. | ||
| P.S. Dacă `num` e `null` atunci `num <= 100` e `true`, așa că fără a 2-a verificarea bucla nu s-ar mai putea opri decât dacă utilizatorul apasă Anulează. Ambele verificări sunt obligatorii. |
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
26 changes: 13 additions & 13 deletions
26
1-js/02-first-steps/13-while-for/7-list-primes/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,29 +1,29 @@ | ||
| There are many algorithms for this task. | ||
| Există mulți algoritmi pentru aceast exercițiu. | ||
|
|
||
| Let's use a nested loop: | ||
| Hai să folosim o buclă imbricată (nested loop): | ||
|
|
||
| ```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 | ||
| Pentru fiecare i din interval { | ||
| verifică dacă i are un divizor de la 1 la i | ||
| dacă da => numărul nu este prim | ||
| dacă nu => numărul este prim, afișează-l | ||
| } | ||
| ``` | ||
|
|
||
| The code using a label: | ||
| Codul folosind o etichetă: | ||
|
|
||
| ```js run | ||
| let n = 10; | ||
|
|
||
| nextPrime: | ||
| for (let i = 2; i <= n; i++) { // for each i... | ||
| următorulNrPrim: | ||
| for (let i = 2; i <= n; i++) { // pentru fiecare 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++) { // caută un divizor.. | ||
| if (i % j == 0) continue următorulNrPrim; // nu e prim, treci la următorul i | ||
| } | ||
|
|
||
| alert( i ); // a prime | ||
| alert( i ); // e prim | ||
| } | ||
| ``` | ||
|
|
||
| There's a lot of space to optimize 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. | ||
| Aici există foarte multe oportunități de optimizare. De exemplu, am putea căuta divizorii de la `2` la radical din `i`. În orice caz, dacă vrem să fim extrem de eficienți pentru intervale mari, trebuie să schimbăm abordarea și să ne bazăm pe matematică avansată și algoritmi complecși precum [Quadratic sieve](https://en.wikipedia.org/wiki/Quadratic_sieve), [General number field sieve](https://en.wikipedia.org/wiki/General_number_field_sieve), 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.
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.
spațiile dintre linii trebuie lăsate cum sunt, altfel nu se văd bine diferențele dintre texte. vezi git diff