Skip to content

Commit 4f1387b

Browse files
Merge pull request #104 from dandgerson/1-js/02-first-steps/12-while-for
translation 1-js/02-first-steps/12-while-for
2 parents 1b07658 + e9bcd60 commit 4f1387b

File tree

14 files changed

+188
-191
lines changed

14 files changed

+188
-191
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The answer: `1`.
1+
Ответ: `1`.
22

33
```js run
44
let i = 3;
@@ -8,18 +8,18 @@ while (i) {
88
}
99
```
1010

11-
Every loop iteration decreases `i` by `1`. The check `while(i)` stops the loop when `i = 0`.
11+
Каждое выполнение цикла уменьшает `i`. Проверка `while(i)` остановит цикл при `i = 0`.
1212

13-
Hence, the steps of the loop form the following sequence ("loop unrolled"):
13+
Соответственно, будет такая последовательность шагов цикла ("развернём" цикл):
1414

1515
```js
1616
let i = 3;
1717

18-
alert(i--); // shows 3, decreases i to 2
18+
alert(i--); // выведет 3, затем уменьшит i до 2
1919

20-
alert(i--) // shows 2, decreases i to 1
20+
alert(i--) // выведет 2, затем уменьшит i до 1
2121

22-
alert(i--) // shows 1, decreases i to 0
22+
alert(i--) // выведет 1, затем уменьшит i до 0
2323

24-
// done, while(i) check stops the loop
24+
// все, проверка while(i) не даст выполняться циклу дальше
2525
```

1-js/02-first-steps/12-while-for/1-loop-last-value/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 3
22

33
---
44

5-
# Last loop value
5+
# Последнее значение цикла
66

7-
What is the last value alerted by this code? Why?
7+
Какое последнее значение выведет этот код? Почему?
88

99
```js
1010
let i = 3;
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons.
1+
Задача демонстрирует, как постфиксные/префиксные варианты могут повлиять на результат, когда используются в сравнениях.
22

3-
1. **From 1 to 4**
3+
1. **От 1 до 4**
44

55
```js run
66
let i = 0;
77
while (++i < 5) alert( i );
88
```
99

10-
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`.
10+
Первое значение: `i = 1`, так как операция `++i` сначала увеличит `i`, а потом уже произойдёт сравнение и выполнение `alert`.
1111

12-
Then follow `2, 3, 4…` -- the values show up one after another. The comparison always uses the incremented value, because `++` is before the variable.
12+
Далее `2, 3, 4…` Значения выводятся одно за другим. Для каждого значения сначала происходит увеличение, а потом – сравнение, так как `++` стоит перед переменной.
1313

14-
Finally, `i = 4` is incremented to `5`, the comparison `while(5 < 5)` fails, and the loop stops. So `5` is not shown.
15-
2. **From 1 to 5**
14+
При `i = 4` произойдет увеличение `i` до `5`, а потом сравнение `while (5 < 5)` – это неверно. Поэтому на этом цикл остановится, и значение `5` выведено не будет.
15+
2. **От 1 до 5**
1616

1717
```js run
1818
let i = 0;
1919
while (i++ < 5) alert( i );
2020
```
2121

22-
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`).
22+
Первое значение: `i = 1`. Остановимся на нём подробнее. Оператор `i++` увеличивает `i`, возвращая старое значение, так что в сравнении `i++ < 5` будет участвовать старое `i = 0`.
2323

24-
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`.
24+
Но последующий вызов `alert` уже не относится к этому выражению, так что получит новый `i = 1`.
2525

26-
Then follow `2, 3, 4…`
26+
Далее `2, 3, 4…` Для каждого значения сначала происходит сравнение, а потом – увеличение, и затем срабатывание `alert`.
2727

28-
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`.
28+
Окончание цикла: при `i = 4` произойдет сравнение `while (4 < 5)` – верно, после этого сработает `i++`, увеличив `i` до `5`, так что значение `5` будет выведено. Оно станет последним.
2929

30-
The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false.
30+
Значение `i = 5` последнее, потому что на следующем шаге `while (5 < 5)` ложно.

1-js/02-first-steps/12-while-for/2-which-value-while/task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ importance: 4
22

33
---
44

5-
# Which values does the while loop show?
5+
# Какие значения выведет цикл while?
66

7-
For every loop iteration, write down which value it outputs and then compare it with the solution.
7+
Для каждого цикла запишите, какие значения он выведет. Потом сравните с ответом.
88

9-
Both loops `alert` the same values, or not?
9+
Оба цикла выводят `alert` с одинаковыми значениями или нет?
1010

11-
1. The prefix form `++i`:
11+
1. Префиксный вариант `++i`:
1212

1313
```js
1414
let i = 0;
1515
while (++i < 5) alert( i );
1616
```
17-
2. The postfix form `i++`
17+
2. Постфиксный вариант `i++`
1818

1919
```js
2020
let i = 0;
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
**The answer: from `0` to `4` in both cases.**
1+
**Ответ: от `0` до `4` в обоих случаях.**
22

33
```js run
44
for (let i = 0; i < 5; ++i) alert( i );
55

66
for (let i = 0; i < 5; i++) alert( i );
77
```
88

9-
That can be easily deducted from the algorithm of `for`:
9+
Такой результат обусловлен алгоритмом работы `for`:
1010

11-
1. Execute once `i = 0` before everything (begin).
12-
2. Check the condition `i < 5`
13-
3. If `true` -- execute the loop body `alert(i)`, and then `i++`
11+
1. Выполнить единожды присваивание `i = 0` перед чем-либо (начало).
12+
2. Проверить условие `i < 5`
13+
3. Если `true` -- выполнить тело цикла `alert(i)`, и затем `i++`
1414

15-
The increment `i++` is separated from the condition check (2). That's just another statement.
16-
17-
The value returned by the increment is not used here, so there's no difference between `i++` and `++i`.
15+
Увеличение `i++` выполняется отдельно от проверки условия `(2)`, значение `i` при этом не используется, поэтому нет никакой разницы между `i++` и `++i`.

1-js/02-first-steps/12-while-for/3-which-value-for/task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ importance: 4
22

33
---
44

5-
# Which values get shown by the "for" loop?
5+
# Какие значения выведет цикл for?
66

7-
For each loop write down which values it is going to show. Then compare with the answer.
7+
Для каждого цикла запишите, какие значения он выведет. Потом сравните с ответом.
88

9-
Both loops `alert` same values or not?
9+
Оба цикла выведут `alert` с одинаковыми значениями или нет?
1010

11-
1. The postfix form:
11+
1. Постфиксная форма:
1212

1313
```js
1414
for (let i = 0; i < 5; i++) alert( i );
1515
```
16-
2. The prefix form:
16+
2. Префиксная форма:
1717

1818
```js
1919
for (let i = 0; i < 5; ++i) alert( i );

1-js/02-first-steps/12-while-for/4-for-even/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ for (let i = 2; i <= 10; i++) {
88
}
99
```
1010

11-
We use the "modulo" operator `%` to get the remainder and check for the evenness here.
11+
Для проверки на чётность мы здесь используем оператор получения остатка от деления `%`.

1-js/02-first-steps/12-while-for/4-for-even/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ importance: 5
22

33
---
44

5-
# Output even numbers in the loop
5+
# Выведите чётные числа
66

7-
Use the `for` loop to output even numbers from `2` to `10`.
7+
При помощи цикла `for` выведите чётные числа от `2` до `10`.
88

99
[demo]

1-js/02-first-steps/12-while-for/5-replace-for-while/task.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ importance: 5
22

33
---
44

5-
# Replace "for" with "while"
5+
# Замените for на while
66

7-
Rewrite the code changing the `for` loop to `while` without altering its behavior (the output should stay same).
7+
Перепишите код, заменив цикл `for` на `while`, без изменения поведения цикла.
88

99
```js run
1010
for (let i = 0; i < 3; i++) {
1111
alert( `number ${i}!` );
1212
}
1313
```
14-

1-js/02-first-steps/12-while-for/6-repeat-until-correct/solution.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ do {
77
} while (num <= 100 && num);
88
```
99

10-
The loop `do..while` repeats while both checks are truthy:
10+
Цикл `do..while` повторяется, пока верны две проверки:
1111

12-
1. The check for `num <= 100` -- that is, the entered value is still not greater than `100`.
13-
2. The check `&& num` is false when `num` is `null` or a empty string. Then the `while` loop stops too.
12+
1. Проверка `num <= 100` -- то есть, введённое число всё еще меньше `100`.
13+
2. Проверка `&& num` вычисляется в `false`, когда `num` имеет значение `null` или пустая строка `''`. В этом случае цикл `while` тоже нужно прекратить.
1414

15-
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.
15+
Кстати, сравнение `num <= 100` при вводе `null` даст `true`, так что вторая проверка необходима.

0 commit comments

Comments
 (0)