Skip to content

Commit e294ee2

Browse files
Numbers (#62)
* feat: translation 50% of Numbers * feat: translation 100% of Numbers * Update 1-js/05-data-types/02-number/article.md Co-Authored-By: Jason Huang <[email protected]>
1 parent df109f5 commit e294ee2

File tree

13 files changed

+214
-215
lines changed

13 files changed

+214
-215
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
32
```js run demo
43
let a = +prompt("The first number?", "");
54
let b = +prompt("The second number?", "");
65

76
alert( a + b );
87
```
98

10-
Note the unary plus `+` before `prompt`. It immediately converts the value to a number.
9+
注意在 `prompt` 之前的一元正號 `+`,它將值立刻轉換為數值。
10+
11+
否則,`a``b` 是個字串,所以它們的加總將會是字串連接,也就是:`"1" + "2" = "12"`
1112

12-
Otherwise, `a` and `b` would be string their sum would be their concatenation, that is: `"1" + "2" = "12"`.

1-js/05-data-types/02-number/1-sum-interface/task.md

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

33
---
44

5-
# Sum numbers from the visitor
5+
# 加總由訪問者輸入的數值
66

7-
Create a script that prompts the visitor to enter two numbers and then shows their sum.
7+
建立一個腳本,提示(prompt)訪問者輸入兩個數值,然後顯示它們的總和。
88

99
[demo]
1010

11-
P.S. There is a gotcha with types.
11+
註:類型會有個問題。
12+

1-js/05-data-types/02-number/2-why-rounded-down/solution.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
Internally the decimal fraction `6.35` is an endless binary. As always in such cases, it is stored with a precision loss.
1+
在十進位分數 `6.35` 的內部是個無窮二進位,在這種情況下,它的存放會有著精度損失。
22

3-
Let's see:
3+
來看這:
44

55
```js run
66
alert( 6.35.toFixed(20) ); // 6.34999999999999964473
77
```
88

9-
The precision loss can cause both increase and decrease of a number. In this particular case the number becomes a tiny bit less, that's why it rounded down.
9+
精度損失可能會導致數值的增減,在這個特殊情況下,數值變得稍微小了點,這就是為什麼它被向下進位了。
1010

11-
And what's for `1.35`?
11+
`1.35` 呢?
1212

1313
```js run
1414
alert( 1.35.toFixed(20) ); // 1.35000000000000008882
1515
```
1616

17-
Here the precision loss made the number a little bit greater, so it rounded up.
17+
精度損失讓該數值稍微大了些,所以它被向上進位。
1818

19-
**How can we fix the problem with `6.35` if we want it to be rounded the right way?**
19+
**若我們想要它正確的進位,該如何修正 `6.35` 的這個問題?**
2020

21-
We should bring it closer to an integer prior to rounding:
21+
我們應該在進位前讓它更靠近整數:
2222

2323
```js run
2424
alert( (6.35 * 10).toFixed(20) ); // 63.50000000000000000000
2525
```
2626

27-
Note that `63.5` has no precision loss at all. That's because the decimal part `0.5` is actually `1/2`. Fractions divided by powers of `2` are exactly represented in the binary system, now we can round it:
28-
27+
注意這個 `63.5` 完全沒有精度損失,這是因為小數部分的 `0.5` 事實上為 `1/2`。被 `2` 的次方所除的除法可以在二進位系統中被完全表示出來,現在我們可以進位它了:
2928

3029
```js run
3130
alert( Math.round(6.35 * 10) / 10); // 6.35 -> 63.5 -> 64(rounded) -> 6.4

1-js/05-data-types/02-number/2-why-rounded-down/task.md

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

33
---
44

5-
# Why 6.35.toFixed(1) == 6.3?
5+
# 為什麼 6.35.toFixed(1) == 6.3
66

7-
According to the documentation `Math.round` and `toFixed` both round to the nearest number: `0..4` lead down while `5..9` lead up.
7+
根據文件 `Math.round` `toFixed` 兩者皆進位到最近的數值:`0..4` 向下捨去,而 `5..9` 向上提升。
88

9-
For instance:
9+
舉個例:
1010

1111
```js run
1212
alert( 1.35.toFixed(1) ); // 1.4
1313
```
1414

15-
In the similar example below, why is `6.35` rounded to `6.3`, not `6.4`?
15+
跟上面類似的例子中,為什麼 `6.35` 會進位為 `6.3` 而非 `6.4`
1616

1717
```js run
1818
alert( 6.35.toFixed(1) ); // 6.3
1919
```
2020

21-
How to round `6.35` the right way?
21+
要如何正確地進位到 `6.35` 呢?
2222

1-js/05-data-types/02-number/3-repeat-until-number/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ function readNumber() {
1515
alert(`Read: ${readNumber()}`);
1616
```
1717

18-
The solution is a little bit more intricate that it could be because we need to handle `null`/empty lines.
18+
解法較為複雜些,因為我們需要處理 `null`/空行。
1919

20-
So we actually accept the input until it is a "regular number". Both `null` (cancel) and empty line also fit that condition, because in numeric form they are `0`.
20+
因此我們實際上一直接收輸入的東西,直到它是個 "正常的數值" 為止。`null`(取消)和空行這兩者也都符合此條件,因為在數值格式中它們都是 `0`
2121

22-
After we stopped, we need to treat `null` and empty line specially (return `null`), because converting them to a number would return `0`.
22+
在我們停下來之後,需要是對 `null` 和空行特別對待(return `null`),因為轉換它們為數值將會回傳 `0`
2323

1-js/05-data-types/02-number/3-repeat-until-number/task.md

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

33
---
44

5-
# Repeat until the input is a number
5+
# 重複動作直到輸入的是個數值為止
66

7-
Create a function `readNumber` which prompts for a number until the visitor enters a valid numeric value.
7+
建立一個函式 `readNumber` 以提示(prompt)輸入數值,直到訪問者真的輸入有效數值為止。
88

9-
The resulting value must be returned as a number.
9+
結果值必須以數值回傳。
1010

11-
The visitor can also stop the process by entering an empty line or pressing "CANCEL". In that case, the function should return `null`.
11+
訪問者也可以透過輸入空行或按下 "CANCEL" 來停止程序。在這個情況下,函式應該要回傳 `null`
1212

1313
[demo]
1414

1-js/05-data-types/02-number/4-endless-loop-error/solution.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
That's because `i` would never equal `10`.
1+
這是因為 `i` 永遠不會等於 `10`
22

3-
Run it to see the *real* values of `i`:
3+
執行這段來看看 `i` *真實* 的值:
44

55
```js run
66
let i = 0;
@@ -10,8 +10,9 @@ while (i < 11) {
1010
}
1111
```
1212

13-
None of them is exactly `10`.
13+
它們之中沒有恰好為 `10` 的值。
1414

15-
Such things happen because of the precision losses when adding fractions like `0.2`.
15+
這種事情發生於因為加上像是`0.2` 這樣的分數,而導致的精度損失。
16+
17+
結論:在使用十進位小數時,避免相等性確認。
1618

17-
Conclusion: evade equality checks when working with decimal fractions.

1-js/05-data-types/02-number/4-endless-loop-error/task.md

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

33
---
44

5-
# An occasional infinite loop
5+
# 偶發的無窮迴圈
66

7-
This loop is infinite. It never ends. Why?
7+
這個迴圈是無窮的,它永不停止。為什麼?
88

99
```js
1010
let i = 0;

1-js/05-data-types/02-number/8-random-min-max/solution.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
We need to "map" all values from the interval 0..1 into values from `min` to `max`.
1+
我們需要 "對應" 0..1 區間內的所有值至 `min` `max` 之間的值上。
22

3-
That can be done in two stages:
3+
可在兩步驟內完成:
44

5-
1. If we multiply a random number from 0..1 by `max-min`, then the interval of possible values increases `0..1` to `0..max-min`.
6-
2. Now if we add `min`, the possible interval becomes from `min` to `max`.
5+
1. 若我們對 `max-min` 乘上一個在 0..1 之間的隨機數,則可能值的區間會由 `0..1` 增加至 `0..max-min`
6+
2. 現在若我們加上 `min`,則區間將變成 `min` `max`
77

8-
The function:
8+
該函式:
99

1010
```js run
1111
function random(min, max) {

1-js/05-data-types/02-number/8-random-min-max/task.md

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

33
---
44

5-
# A random number from min to max
5+
# min max 的隨機數
66

7-
The built-in function `Math.random()` creates a random value from `0` to `1` (not including `1`).
7+
內建函式 `Math.random()` 建立一個從 `0` `1` 的隨機值(不包括 `1`)。
88

9-
Write the function `random(min, max)` to generate a random floating-point number from `min` to `max` (not including `max`).
9+
寫個函式 `random(min, max)` `min` `max` 中產生隨機的浮點數(不包括 `max`)。
1010

11-
Examples of its work:
11+
可行的範例:
1212

1313
```js
1414
alert( random(1, 5) ); // 1.2345623452
1515
alert( random(1, 5) ); // 3.7894332423
1616
alert( random(1, 5) ); // 4.3435234525
1717
```
18+

0 commit comments

Comments
 (0)