-
Notifications
You must be signed in to change notification settings - Fork 9
Loops: while and for #84
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
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
7af7b49
article while loops
vahmelk99 8f4f536
break translated
vahmelk99 794be86
article done
vahmelk99 371b28c
ex1 translated
vahmelk99 88e3ddd
exs translated
vahmelk99 0ae628f
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 feb54b2
Update 1-js/02-first-steps/13-while-for/2-which-value-while/solution.md
vahmelk99 3cc0808
Update 1-js/02-first-steps/13-while-for/5-replace-for-while/task.md
vahmelk99 998b003
Update 1-js/02-first-steps/13-while-for/6-repeat-until-correct/soluti…
vahmelk99 763c6f2
Update 1-js/02-first-steps/13-while-for/7-list-primes/solution.md
vahmelk99 ffb0e12
Update 1-js/02-first-steps/13-while-for/7-list-primes/task.md
vahmelk99 38730de
7th sol fix
vahmelk99 9bb925b
Merge branch 'master' into master
vahmelk99 5806f13
Update 1-js/02-first-steps/13-while-for/7-list-primes/solution.md
vahmelk99 e653e50
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 6f701c6
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 df1e71f
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 9f90054
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 14c423e
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 4ccbb8f
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 6482c5d
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 3f2c9c8
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 132ebde
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 8cef7f2
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 4927262
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 a84d5d3
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 74574c7
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 a76316c
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 b032a1f
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 6ba30b1
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 2de07d1
Update 1-js/02-first-steps/13-while-for/article.md
vahmelk99 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
22 changes: 11 additions & 11 deletions
22
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,30 @@ | ||
The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons. | ||
Այս առաջադրանքը ցուց է տալիս, թե ինչպես նախածանցի/վերջածանցի կիրառումը կարող է բերել տարբեր արդյունքների, երբ դրանք օգտագործվում են համեմատության մեջ։ | ||
|
||
1. **From 1 to 4** | ||
1. **1֊ից մինչև 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`. | ||
Առաջին արժեքը `i = 1`, քանի որ `++i`֊ն սկզբում աճեցնում է `i`֊ի արժեքը, և հետո նոր վերադարձնում ստացված նոր աժեքը։ Այսպիսով առաջին համեմատությունը կլինի `1 < 5` և `alert`֊ը ցույց կտա `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. | ||
Ապա կհաջորդի `2, 3, 4…` -- արժեքները կցուցադրվեն մեկը մյուսի հետևից։ Համեմատումը միշտ օգտագործում է ավելացված արժեքը, քանի որ `++`֊ը գրված է փոփոխականից առաջ։ | ||
|
||
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** | ||
Եվ վերջապես `i = 4` ավելացվում է դառնալով `5`, `while(5 < 5)` համեմատումը կլինի սխալ, և ցիկլը կավարտվի։ Այսպիսով `5`֊ը չի երևա էկրանին։ | ||
2. **1֊ից մինչև 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`). | ||
Առաջին արժեքը կրկին `i = 1` է։ `i++`֊ը աճեցնում է `i`֊ն, և ապա վերադարձնում *հին* արժեքը, այսպիսով `i++ < 5` համեմատության մեջ `i = 0` (ի տարբերություն `++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`. | ||
Բայց `alert`֊ի կանչը առանձին է։ Այն առանձին հատված է, որը կատարվում է փոփոխականի աճից և համեմատումից հետո։ Այսպիսով այն ստանում է ընթացիկ `i = 1`։ | ||
|
||
Then follow `2, 3, 4…` | ||
Ապա հետևում են `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`. | ||
Կանգ առնենք `i = 4` դեպքի վրա։ `++i`֊ն կաճեցներ `i`֊ն և կօգտագործեր `5` արժեքը համեմատման համար։ Բայց այստեղ օգտագործվում է `i++`, որը աճեցնում է `i`֊ն՝ դանձնելով `5`, և վերադարձնում հին արժեքը։ Հետևաբար համեմատումը կլինի `while(4 < 5)` -- որն էլ ճիշտ է, և `alert`֊ը ցույց կտա `5` արժեքը։ | ||
|
||
The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false. | ||
`i = 5` արժեքի դեպքում հաջորդ քայլում կունենանք `while(5 < 5)`, որն էլ սխալ է։ |
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/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.** | ||
**Պատասխան՝ `0`֊ից մինչև `4`, երկու դեպքում էլ։** | ||
|
||
```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`: | ||
Այս պատասխանին կարող ենք հասնել դիտարկելով `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. Կատարում է մի անգամ `i = 0`, ամեն ինչից առաջ (սկիզբ)։ | ||
2. Ստուգում է պայմանը `i < 5`։ | ||
3. Եթե ճիշտ է (`true`) -- կատարում է ցիկլի մարմինը՝ `alert(i)`, և ապա `i++` | ||
|
||
The increment `i++` is separated from the condition check (2). That's just another statement. | ||
`i++` աճը բաժանված է պայմանի ստուգման գործողությունից (2)։ Այն կոդի ուրիշ անկախ հատված է։ | ||
|
||
The value returned by the increment is not used here, so there's no difference between `i++` and `++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
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( `Համար ${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/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. | ||
Կան շատ տարբեր ալգորիթմներ այս խնդրիը լուծելու համար։ | ||
|
||
Let's use a 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 | ||
Բոլոր i֊երի համար հատվածից { | ||
ստուգում է, արդյոք i֊ն բաժանվում է ինչ֊որ թվի միչև ինքը | ||
եթե այո => արժեքը պարզ չէ | ||
եթե ոչ => արժեքը պարզ է և պետք է ցույց տալ այն | ||
} | ||
``` | ||
|
||
The code using a label: | ||
Ծրագիրը օգտագործում է պիտակ (label)․ | ||
|
||
```js run | ||
let n = 10; | ||
|
||
nextPrime: | ||
for (let i = 2; i <= n; i++) { // for each i... | ||
for (let i = 2; i <= n; i++) { // ամեն 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++) { // փնտրում է բաժանարար.. | ||
if (i % j == 0) continue nextPrime; // պարզ չէ, անցնում է հաջորդ i֊ին | ||
} | ||
|
||
alert( i ); // a prime | ||
alert( i ); // պարզ թիվ | ||
} | ||
``` | ||
|
||
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. | ||
Այս օրինակում կան շատ տեղեր, որ կարելի է ավելի օպտիմալ գրել։ Օրինակ մենք կարող ենք փնտրել բաժանարարները `2`֊ից մինչև արմատ `i`֊ն։ Մեծ ինտերվալների դեպքում մենք պետք է լավացնենք (այսինքն փոքրացնենք) փնտրման սահմանները, ավելի արդյունավետ ծրագիր ստանալու համար։ Դրա համար պետք է ծանոթ լինենք խորացված մաթեմատիկայի տարրերին և կոմպլեքս ալգորիթմներին, ինչպիսիք են [Քառակուսային մաղ (Quadratic sieve)](https://en.wikipedia.org/wiki/Quadratic_sieve), [Ընդհանուր թվերի դաշտային մաղ (General number field sieve)](https://en.wikipedia.org/wiki/General_number_field_sieve) և այլն։ |
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.