File tree Expand file tree Collapse file tree 13 files changed +214
-215
lines changed
1-js/05-data-types/02-number Expand file tree Collapse file tree 13 files changed +214
-215
lines changed Original file line number Diff line number Diff line change 1
1
2
-
3
2
``` js run demo
4
3
let a = + prompt (" The first number?" , " " );
5
4
let b = + prompt (" The second number?" , " " );
6
5
7
6
alert ( a + b );
8
7
```
9
8
10
- Note the unary plus ` + ` before ` prompt ` . It immediately converts the value to a number.
9
+ 注意在 ` prompt ` 之前的一元正號 ` + ` ,它將值立刻轉換為數值。
10
+
11
+ 否則,` a ` 和 ` b ` 是個字串,所以它們的加總將會是字串連接,也就是:` "1" + "2" = "12" ` 。
11
12
12
- Otherwise, ` a ` and ` b ` would be string their sum would be their concatenation, that is: ` "1" + "2" = "12" ` .
Original file line number Diff line number Diff line change @@ -2,10 +2,11 @@ importance: 5
2
2
3
3
---
4
4
5
- # Sum numbers from the visitor
5
+ # 加總由訪問者輸入的數值
6
6
7
- Create a script that prompts the visitor to enter two numbers and then shows their sum.
7
+ 建立一個腳本,提示(prompt)訪問者輸入兩個數值,然後顯示它們的總和。
8
8
9
9
[ demo]
10
10
11
- P.S. There is a gotcha with types.
11
+ 註:類型會有個問題。
12
+
Original file line number Diff line number Diff line change 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 ` 的內部是個無窮二進位,在這種情況下,它的存放會有著精度損失。
2
2
3
- Let's see:
3
+ 來看這:
4
4
5
5
``` js run
6
6
alert ( 6.35 .toFixed (20 ) ); // 6.34999999999999964473
7
7
```
8
8
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
+ 精度損失可能會導致數值的增減,在這個特殊情況下,數值變得稍微小了點,這就是為什麼它被向下進位了。
10
10
11
- And what's for ` 1.35 ` ?
11
+ 而 ` 1.35 ` 呢?
12
12
13
13
``` js run
14
14
alert ( 1.35 .toFixed (20 ) ); // 1.35000000000000008882
15
15
```
16
16
17
- Here the precision loss made the number a little bit greater, so it rounded up.
17
+ 精度損失讓該數值稍微大了些,所以它被向上進位。
18
18
19
- ** How can we fix the problem with ` 6.35 ` if we want it to be rounded the right way? **
19
+ ** 若我們想要它正確的進位,該如何修正 ` 6.35 ` 的這個問題? **
20
20
21
- We should bring it closer to an integer prior to rounding:
21
+ 我們應該在進位前讓它更靠近整數:
22
22
23
23
``` js run
24
24
alert ( (6.35 * 10 ).toFixed (20 ) ); // 63.50000000000000000000
25
25
```
26
26
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 ` 的次方所除的除法可以在二進位系統中被完全表示出來,現在我們可以進位它了:
29
28
30
29
``` js run
31
30
alert ( Math .round (6.35 * 10 ) / 10 ); // 6.35 -> 63.5 -> 64(rounded) -> 6.4
Original file line number Diff line number Diff line change @@ -2,21 +2,21 @@ importance: 4
2
2
3
3
---
4
4
5
- # Why 6.35.toFixed(1) == 6.3?
5
+ # 為什麼 6.35.toFixed(1) == 6.3?
6
6
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 ` 向上提升。
8
8
9
- For instance:
9
+ 舉個例:
10
10
11
11
``` js run
12
12
alert ( 1.35 .toFixed (1 ) ); // 1.4
13
13
```
14
14
15
- In the similar example below, why is ` 6.35 ` rounded to ` 6.3 ` , not ` 6.4 ` ?
15
+ 跟上面類似的例子中,為什麼 ` 6.35 ` 會進位為 ` 6.3 ` 而非 ` 6.4 ` ?
16
16
17
17
``` js run
18
18
alert ( 6.35 .toFixed (1 ) ); // 6.3
19
19
```
20
20
21
- How to round ` 6.35 ` the right way?
21
+ 要如何正確地進位到 ` 6.35 ` 呢?
22
22
Original file line number Diff line number Diff line change @@ -15,9 +15,9 @@ function readNumber() {
15
15
alert (` Read: ${ readNumber ()} ` );
16
16
```
17
17
18
- The solution is a little bit more intricate that it could be because we need to handle ` null ` /empty lines.
18
+ 解法較為複雜些,因為我們需要處理 ` null ` /空行。
19
19
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 ` 。
21
21
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 ` 。
23
23
Original file line number Diff line number Diff line change @@ -2,13 +2,13 @@ importance: 5
2
2
3
3
---
4
4
5
- # Repeat until the input is a number
5
+ # 重複動作直到輸入的是個數值為止
6
6
7
- Create a function ` readNumber ` which prompts for a number until the visitor enters a valid numeric value.
7
+ 建立一個函式 ` readNumber ` 以提示(prompt)輸入數值,直到訪問者真的輸入有效數值為止。
8
8
9
- The resulting value must be returned as a number.
9
+ 結果值必須以數值回傳。
10
10
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 ` 。
12
12
13
13
[ demo]
14
14
Original file line number Diff line number Diff line change 1
- That's because ` i ` would never equal ` 10 ` .
1
+ 這是因為 ` i ` 永遠不會等於 ` 10 ` 。
2
2
3
- Run it to see the * real * values of ` i ` :
3
+ 執行這段來看看 ` i ` * 真實 * 的值:
4
4
5
5
``` js run
6
6
let i = 0 ;
@@ -10,8 +10,9 @@ while (i < 11) {
10
10
}
11
11
```
12
12
13
- None of them is exactly ` 10 ` .
13
+ 它們之中沒有恰好為 ` 10 ` 的值。
14
14
15
- Such things happen because of the precision losses when adding fractions like ` 0.2 ` .
15
+ 這種事情發生於因為加上像是` 0.2 ` 這樣的分數,而導致的精度損失。
16
+
17
+ 結論:在使用十進位小數時,避免相等性確認。
16
18
17
- Conclusion: evade equality checks when working with decimal fractions.
Original file line number Diff line number Diff line change @@ -2,9 +2,9 @@ importance: 4
2
2
3
3
---
4
4
5
- # An occasional infinite loop
5
+ # 偶發的無窮迴圈
6
6
7
- This loop is infinite. It never ends. Why?
7
+ 這個迴圈是無窮的,它永不停止。為什麼?
8
8
9
9
``` js
10
10
let i = 0 ;
Original file line number Diff line number Diff line change 1
- We need to "map" all values from the interval 0..1 into values from ` min ` to ` max ` .
1
+ 我們需要 "對應" 0..1 區間內的所有值至 ` min ` 到 ` max ` 之間的值上。
2
2
3
- That can be done in two stages:
3
+ 可在兩步驟內完成:
4
4
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 ` 。
7
7
8
- The function:
8
+ 該函式:
9
9
10
10
``` js run
11
11
function random (min , max ) {
Original file line number Diff line number Diff line change @@ -2,16 +2,17 @@ importance: 2
2
2
3
3
---
4
4
5
- # A random number from min to max
5
+ # 由 min 至 max 的隨機數
6
6
7
- The built-in function ` Math.random() ` creates a random value from ` 0 ` to ` 1 ` (not including ` 1 ` ).
7
+ 內建函式 ` Math.random() ` 建立一個從 ` 0 ` 到 ` 1 ` 的隨機值(不包括 ` 1 ` )。
8
8
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 ` )。
10
10
11
- Examples of its work:
11
+ 可行的範例:
12
12
13
13
``` js
14
14
alert ( random (1 , 5 ) ); // 1.2345623452
15
15
alert ( random (1 , 5 ) ); // 3.7894332423
16
16
alert ( random (1 , 5 ) ); // 4.3435234525
17
17
```
18
+
You can’t perform that action at this time.
0 commit comments