Skip to content

Commit 6baabac

Browse files
committed
up
1 parent f3886cf commit 6baabac

File tree

41 files changed

+196
-163
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+196
-163
lines changed

1-js/2-first-steps/15-while-for/2-which-value-while/solution.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
1+
The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons.
2+
13
<ol>
2-
<li>**От 1 до 4**
4+
<li>**From 1 to 4**
35

46
```js
57
//+ run
68
var i = 0;
79
while (++i < 5) alert( i );
810
```
911

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

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

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

1820
```js
1921
//+ run
2022
var i = 0;
2123
while (i++ < 5) alert( i );
2224
```
2325

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

26-
Но последующий вызов `alert` уже не относится к этому выражению, так что получит новый `i=1`.
30+
Then follow `2,3,4…`
2731

28-
Далее `2,3,4..` Для каждого значения сначала происходит сравнение, а потом -- увеличение, и затем срабатывание `alert`.
32+
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`.
33+
34+
The value `i=5` is the last one, because on the next step `while(5 < 5)` is false.
35+
</li>
36+
</ol>
2937

30-
Окончание цикла: при `i=4` произойдет сравнение `while(4 < 5)` -- верно, после этого сработает `i++`, увеличив `i` до `5`, так что значение `5` будет выведено. Оно станет последним.</li>
31-
</ol>
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
# Какие значения i выведет цикл while?
1+
# Which values shows the while?
22

33
[importance 4]
44

5-
Для каждого цикла запишите, какие значения он выведет. Потом сравните с ответом.
5+
For every loop, scribe down which values it shows, in your opinion.
6+
7+
And then compare with the answer.
8+
69
<ol>
7-
<li>Префиксный вариант
10+
<li>The prefix form `++i`:
811

912
```js
1013
var i = 0;
1114
while (++i < 5) alert( i );
1215
```
1316

1417
</li>
15-
<li>Постфиксный вариант
18+
<li>The postfix form `i++`
1619

1720
```js
1821
var i = 0;
1922
while (i++ < 5) alert( i );
2023
```
2124

2225
</li>
23-
</ol>
26+
</ol>
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
**Ответ: от 0 до 4 в обоих случаях.**
1+
**The answer: from `0` to `4` in both cases.**
22

33
```js
44
//+ run
@@ -7,11 +7,13 @@ for (var i = 0; i < 5; ++i) alert( i );
77
for (var i = 0; i < 5; i++) alert( i );
88
```
99

10-
Такой результат обусловлен алгоритмом работы `for`:
10+
That can be easily deducted from the algorithm of `for`:
1111
<ol>
12-
<li>Выполнить присвоение `i=0`</li>
13-
<li>Проверить `i<5`</li>
14-
<li>Если верно - выполнить тело цикла `alert(i)`, затем выполнить `i++`</li>
12+
<li>Execute once `i=0` before everything.</li>
13+
<li>Check the condition `i<5`</li>
14+
<li>If `true` -- execute the loop body `alert(i)`, and then `i++`</li>
1515
</ol>
1616

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

1-js/2-first-steps/15-while-for/3-which-value-for/task.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
# Какие значения i выведет цикл for?
1+
# Which values get shown by the "for" loop?
22

33
[importance 4]
44

5-
Для каждого цикла запишите, какие значения он выведет. Потом сравните с ответом.
5+
For each loop scribe down which values it is going to show.
6+
7+
Then compare with the answer.
8+
69
<ol>
7-
<li>Постфиксная форма:
10+
<li>The postfix form:
811

912
```js
1013
for (var i = 0; i < 5; i++) alert( i );
1114
```
1215

1316
</li>
14-
<li>Префиксная форма:
17+
<li>The prefix form:
1518

1619
```js
1720
for (var i = 0; i < 5; ++i) alert( i );

1-js/2-first-steps/15-while-for/4-for-even/solution.md

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

12-
Чётность проверяется по остатку при делении на `2`, используя оператор "деление с остатком" `%`: `i % 2`.
12+
We use the "modulo" `%` to get the remainder and check for the evenness here.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Выведите чётные числа
1+
# Output even numbers in the loop
22

33
[importance 5]
44

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

77
[demo /]

1-js/2-first-steps/15-while-for/5-replace-for-while/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//+ run
55
var i = 0;
66
while (i < 3) {
7-
alert( "номер " + i + "!" );
7+
alert( `number ${i}!` );
88
i++;
99
}
1010
```
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Замените for на while
1+
# Replace "for" with "while"
22

33
[importance 5]
44

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

77
```js
88
//+ run
99
for (var i = 0; i < 3; i++) {
10-
alert( "номер " + i + "!" );
10+
alert( `number ${i}!` );
1111
}
1212
```
1313

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11

2-
32
```js
43
//+ run demo
54
var num;
65

76
do {
8-
num = prompt("Введите число больше 100?", 0);
9-
} while (num <= 100 && num != null);
7+
num = prompt("Enter a number greater than 100?", 0);
8+
} while (num <= 100 && num);
109
```
1110

12-
Цикл `do..while` повторяется, пока верны две проверки:
11+
The loop `do..while` repeats while both checks are truthy:
1312
<ol>
14-
<li>Проверка `num <= 100` -- то есть, введённое число всё еще меньше `100`.</li>
15-
<li>Проверка `num != null` -- значение `null` означает, что посетитель нажал "Отмена", в этом случае цикл тоже нужно прекратить.</li>
13+
<li>The check for `num <= 100` -- that is, the entered value is still not greater than `100`.</li>
14+
<li>The check for a truthiness of `num` checks that `num != null` and `num != ""` simultaneously.</li>
1615
</ol>
1716

18-
Кстати, сравнение `num <= 100` при вводе `null` даст `true`, так что вторая проверка необходима.
17+
P.S. By the way, if `num` is `null` then `num <= 100` would return `false`, not `true`!
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Повторять цикл, пока ввод неверен
1+
# Repeat until the input is incorrect
22

33
[importance 5]
44

5-
Напишите цикл, который предлагает `prompt` ввести число, большее `100`. Если посетитель ввёл другое число -- попросить ввести ещё раз, и так далее.
5+
Write a loop which prompts for a number greater than `100`. If the visitor enters another number -- ask him to repeat the input, and so on.
66

7-
Цикл должен спрашивать число пока либо посетитель не введёт число, большее `100`, либо не нажмёт кнопку Cancel (ESC).
7+
The loop must ask for a number until either the visitor enters a number greater than `100` or cancels the input/enters an empty line.
88

9-
Предполагается, что посетитель вводит только числа. Предусматривать обработку нечисловых строк в этой задаче необязательно.
9+
Here we can assume that the visitor only inputs numbers. There's no need to implement the special handling for a non-numeric input in this task.
1010

1111
[demo /]

0 commit comments

Comments
 (0)