Skip to content

Commit a035121

Browse files
lenchen1112ArvinH
andauthored
Loops: while and for (#34)
* feat: translation of Loops: while and for * Update 1-js/02-first-steps/12-while-for/2-which-value-while/solution.md Co-Authored-By: Jason Huang <[email protected]> * Update 1-js/02-first-steps/12-while-for/2-which-value-while/task.md Co-Authored-By: Jason Huang <[email protected]> * Update 1-js/02-first-steps/12-while-for/4-for-even/solution.md Co-Authored-By: Jason Huang <[email protected]> * Update 1-js/02-first-steps/12-while-for/7-list-primes/task.md Co-Authored-By: Jason Huang <[email protected]> * Update 1-js/02-first-steps/12-while-for/1-loop-last-value/solution.md Co-Authored-By: ArvinH <[email protected]> * Update 1-js/02-first-steps/12-while-for/article.md Co-Authored-By: ArvinH <[email protected]> * Update 1-js/02-first-steps/12-while-for/article.md Co-Authored-By: ArvinH <[email protected]>
1 parent 4fdf831 commit a035121

File tree

15 files changed

+191
-181
lines changed

15 files changed

+191
-181
lines changed
Lines changed: 8 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,19 @@ 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` 減少 `1`。當 `i = 0` 時,`while(i)` 這個檢查會停止迴圈。
1212

13-
Hence, the steps of the loop form the following sequence ("loop unrolled"):
13+
因此,迴圈的步驟形成以下序列("循環展開(loop unrolled)"):
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
```
26+

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

Lines changed: 3 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;
@@ -13,3 +13,4 @@ while (i) {
1313
alert( i-- );
1414
}
1515
```
16+

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
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` 再回傳新的值。所以第一個比較是 `1 < 5`,而 `alert` 將顯示 `1`
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` 遞增至 `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`(與 `++i < 5` 不同)。
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...`
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` 停下來。使用前置型式 `++i` 將會遞增並使用 `5` 做比較,但這裡我們使用的是後置型式 `i++`,所以它遞增了 `i``5`,但回傳舊的值。因此事實上比較的是 `while(4 < 5)` -- 為真,故控制權依然給了 `alert`
29+
30+
`i = 5` 是最後一個值了,因為下一步 `while(5 < 5)` 為假。
2931

30-
The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false.

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ 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;
2121
while (i++ < 5) alert( i );
2222
```
23+

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
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. 在開始前(begin),執行一次 `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.
15+
遞增 `i++` 跟條件檢查(2)是分開的,它就是另一個述語。
16+
17+
遞增所回傳的值沒有在這邊被使用到,所以在 `i++``++i` 之間沒有任何區別。
1618

17-
The value returned by the increment is not used here, so there's no difference between `i++` and `++i`.

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ 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 );
2020
```
21+
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
```js run demo
43
for (let i = 2; i <= 10; i++) {
54
if (i % 2 == 0) {
@@ -8,4 +7,5 @@ for (let i = 2; i <= 10; i++) {
87
}
98
```
109

11-
We use the "modulo" operator `%` to get the remainder and check for the evenness here.
10+
我們使用 "模數(modulo)" 運算子 `%` 來得到餘數,並確認是否為偶數。
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/solution.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
```js run
43
let i = 0;
54
while (i < 3) {

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

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

33
---
44

5-
# Replace "for" with "while"
5+
# 使用 "while" 取代 "for"
66

7-
Rewrite the code changing the `for` loop to `while` without altering its behavior (the output should stay same).
7+
使用 `while` 改寫以下使用 `for` 迴圈的程式碼,並且不改變其行為(輸出要維持一樣)。
88

99
```js run
1010
for (let i = 0; i < 3; i++) {

0 commit comments

Comments
 (0)