-
Notifications
You must be signed in to change notification settings - Fork 183
Loops tasks #190
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
Loops tasks #190
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c645df0
Translate tasks for article 'While For'
tarasyyyk 0953183
Update 1-js/02-first-steps/13-while-for/2-which-value-while/solution.md
tarasyyyk b36aaca
Update 1-js/02-first-steps/13-while-for/5-replace-for-while/task.md
tarasyyyk a22c4c5
Update 1-js/02-first-steps/13-while-for/3-which-value-for/task.md
tarasyyyk 6645f39
Update 1-js/02-first-steps/13-while-for/2-which-value-while/task.md
tarasyyyk 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
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` до `1` і повертає *старе* значення, тому порівняння `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` та порівняння. Тому він отримає поточне значення `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`. | ||
|
|
||
| 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++` та `++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` в інтервалі (від 2 до n) { | ||
| перевірити, чи число `i` має дільник з діапазону 2..i | ||
| якщо так => значення не просте | ||
| якщо ні => значення просте, показати його | ||
| } | ||
| ``` | ||
|
|
||
| The code using a 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. | ||
| Звичайно, цей код можна оптимізувати з точки зору продуктивності. Наприклад, ми могли б перевіряти всі `j` від `2` до квадратного кореня з `i`. Але все ж таки, якби потрібно було перебирати справді великі числа, нам прийшлося б використовувати просунуту математику і складні алгоритми на кшталт [квадратичного решета](https://uk.wikipedia.org/wiki/Квадратичне_решето) чи [методу решета числового поля](https://uk.wikipedia.org/wiki/Метод_решета_числового_поля). |
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
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.