Skip to content

Update translation of 1-js/05-data-types/02-number #597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Feb 15, 2020
4 changes: 2 additions & 2 deletions 1-js/05-data-types/02-number/1-sum-interface/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ let b = +prompt("The second number?", "");
alert( a + b );
```

注意一元符号 `+` 在 `prompt` 前面。它会把获取的值转换成数字
注意在 `prompt` 前面的一元加号 `+`。它会立刻把获取的值转换成数字

否则,`a` 和 `b` 会是字符串,它们的总和就是它们的连接,即:`“1”+“2”=“12”`。
否则,`a` 和 `b` 会是字符串,它们的总和就是它们的连接,即:`"1" + "2" = "12"`。
4 changes: 2 additions & 2 deletions 1-js/05-data-types/02-number/1-sum-interface/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ importance: 5

---

# 来自访客的总数
# 来自访客的数字求和

创建一个脚本,提示访问者输入两个数字,然后显示它们的总和。

[demo]

提示:类型会有一个问题
提示:这有一个类型陷阱
12 changes: 6 additions & 6 deletions 1-js/05-data-types/02-number/2-why-rounded-down/solution.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
在 `6.35` 内部,小数部分是一个无限的二进制。在这种情况下,它存储的时候有一个精度的缺失
在 `6.35` 内部,小数部分是一个无限的二进制。在这种情况下,它的存储会存在一个精度的损失

让我们来看看:

```js run
alert( 6.35.toFixed(20) ); // 6.34999999999999964473
```

精度缺失可能会导致数字的增加和减小。在这种特殊情况下,数字可能会变小了一点,这就是为什么它减小了。
精度损失可能会导致数字的增加和减小。在这种特殊情况下,数字可能会变小了一点,这就是为什么它减小了。

那么 `1.35` 是怎样样的呢
那么 `1.35` 是怎样的呢

```js run
alert( 1.35.toFixed(20) ); // 1.35000000000000008882
```

在这里,精度缺失使得这个数字更大一些,所以这个数字变大了一些。
在这里,精度损失使得这个数字更大了一些,所以这个数字变大了一些。

**如果我们希望以正确的方式四舍五入,我们如何使用 `6.35` 为例来解决这个问题?**

在四舍五入之前,我们应该把它接近整数
在四舍五入之前,我们应该使它更接近整数

```js run
alert( (6.35 * 10).toFixed(20) ); // 63.50000000000000000000
```

请注意 `63.5` 完全没有精度缺失。这是因为小数部分 `0.5` 实际上是 `1 / 2`。除以2的幂的分数在二进制系统中被精确地表示,现在我们可以围绕它来解决问题
请注意 `63.5` 完全没有精度缺失。这是因为小数部分 `0.5` 实际上是 `1/2`。以 2 为分母的分数在二进制系统中可以被精确地表示,现在我们可以对它进行四舍五入了


```js run
Expand Down
2 changes: 1 addition & 1 deletion 1-js/05-data-types/02-number/2-why-rounded-down/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ importance: 4

# 为什么 6.35.toFixed(1) == 6.3?

根据文档 `Math.round` 和 `toFixed`,最近的数字四舍五入:`0..4` 会被舍去,而 `5..9` 会前进一位。
根据文档 `Math.round` 和 `toFixed` 均将数字进行四舍五入:`0..4` 会被舍去,而 `5..9` 会前进一位。

例如:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("readNumber", function() {
assert.strictEqual(readNumber(), 0);
});

it("continues the loop unti meets a number", function() {
it("continues the loop until meets a number", function() {
prompt.onCall(0).returns("not a number");
prompt.onCall(1).returns("not a number again");
prompt.onCall(2).returns("1");
Expand All @@ -35,4 +35,4 @@ describe("readNumber", function() {
assert.isNull(readNumber());
});

});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function readNumber() {
alert(`Read: ${readNumber()}`);
```

该解决方案有点复杂,可能是因为我们需要处理 null/空行。
该解决方案有点复杂,可能是因为我们需要处理 `null`/空行。

所以我们实际上接受输入,直到它成为“常规数字”。null(取消)和空行都适合该条件,因为在数字形式中它们是 `0`。
所以我们实际上一直接受输入,直到输入的内容为“常规数字”。`null`(取消)和空行也都适合该条件,因为在数字形式中它们是 `0`。

在我们停止之后,我们需要专门处理 null 和空行(返回 null),因为将它们转换为数字将返回 `0`。
在我们停止之后,我们需要专门处理 `null` 和空行(返回 `null`),因为将它们转换为数字将返回 `0`。
6 changes: 3 additions & 3 deletions 1-js/05-data-types/02-number/3-repeat-until-number/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ importance: 5

---

# 重复检测,直到输入是一个数字
# 重复检测,直到输入的是一个数字

创建一个函数 `readNumber`,它提示输入一个数字,直到访问者输入一个有效的数字
创建一个函数 `readNumber`,它提示输入一个数字,直到访问者输入一个有效的数字为止

结果值必须作为数字返回。

访问者也可以通过输入空行或按“取消”来停止该过程。在这种情况下,函数应该返回 `null`。
访问者也可以通过输入空行或点击“取消”来停止该过程。在这种情况下,函数应该返回 `null`。

[demo]
8 changes: 4 additions & 4 deletions 1-js/05-data-types/02-number/4-endless-loop-error/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
那是因为 `i` 永远不会等于 `10`。

运行它以查看 `i` 的实际值
运行它以查看 `i` 的 **真实** 值

```js run
let i = 0;
Expand All @@ -10,8 +10,8 @@ while (i < 11) {
}
```

他们中没有一个完全是 `10`。
它们中没有一个完全是 `10`。

发生这样的事情是因为在添加像 `0.2` 这样的分数时出现了精度损失
发生这样的事情是因为在添加像 `0.2` 这样的小数时出现了精度损失

结论:在处理小数部分时避免相等检查
结论:在处理小数部分时,应避免相等检查
6 changes: 3 additions & 3 deletions 1-js/05-data-types/02-number/8-random-min-max/solution.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
我们需要将区间 0..1 中的所有值“映射”为从最小值到最大值
我们需要将区间 0..1 中的所有值“映射”为从 `min` 到 `max`

这可以分两个阶段完成:

1. 如果我们将 0..1 的随机数乘以 `max-min`,则可能值的间隔从 0..1 增加到 `0..max-min`。
2. 现在,如果我们添加 `min`,则可能的间隔将从 `min` 变为 `max`。
1. 如果我们将 0..1 的随机数乘以 `max-min`,则随机数的取值范围从 0..1 增加到 `0..max-min`。
2. 现在,如果我们将 `min` 值与随机数相加,则随机数的取值范围就变为了从 `min` `max`。

函数实现:

Expand Down
6 changes: 3 additions & 3 deletions 1-js/05-data-types/02-number/8-random-min-max/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 2

---

# 从最小到最大的随机数
# 从 min 到 max 的随机数

用内置函数Math.random() 创建一个从 01 的随机值(不包括 1 )。
内置函数 `Math.random()` 会创建一个 `0``1` 的随机值(不包括 `1`)。

编写随机函数(minmax)以生成从最小到最大(不包括最大值)的随机浮点数。
编写函数 `random(min, max)` 以生成从 `min` 到 `max`(不包括 `max`)的随机浮点数。

实例:

Expand Down
16 changes: 8 additions & 8 deletions 1-js/05-data-types/02-number/9-random-int-min-max/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 简单但错误的解决方案

最简单但错误的解决方案是从 `min` 到 `max` 生成一个值并取它四舍五入的值
最简单但错误的解决方案是生成一个范围在 `min` 到 `max` 的值,并取对其进行四舍五入后的值

```js run
function randomInteger(min, max) {
Expand All @@ -11,11 +11,11 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```

这个函数是能起作用的,但不正确。获得边缘值 `min` 和 `max` 的概率是其他值的两倍
这个函数是能起作用的,但不正确。获得边缘值 `min` 和 `max` 的概率比其他值低两倍

如果你多次运行这个例子,你会很容易看到 `2` 出现的频率最高。
如果你将上面这个例子运行多次,你会很容易看到 `2` 出现的频率最高。

发生这种情况是因为 `Math.round()` 从间隔 `1..3` 获得随机数并按如下所示进行四舍五入
发生这种情况是因为 `Math.round()` 从范围 `1..3` 中获得随机数,并按如下所示进行四舍五入

```js no-beautify
values from 1 ... to 1.4999999999 become 1
Expand All @@ -27,12 +27,12 @@ values from 2.5 ... to 2.9999999999 become 3

# 正确的解决方案

这项任务有很多正确的解决方案。其中之一是调整间隔边界。为了确保相同的时间间隔,我们可以生成从 0.5 到 3.5 的值,从而将所需的概率添加到边缘
这个题目有很多正确的解决方案。其中之一是调整取值范围的边界。为了确保相同的取值范围,我们可以生成从 0.5 到 3.5 的值,从而将所需的概率添加到取值范围的边界

```js run
*!*
function randomInteger(min, max) {
// now rand is from (min-0.5) to (max+0.5)
// 现在范围是从 (min-0.5) (max+0.5)
let rand = min - 0.5 + Math.random() * (max - min + 1);
return Math.round(rand);
}
Expand All @@ -41,7 +41,7 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```

另一种方法是使用 `Math.floor` 作为从 `min` 到 `max+1` 的随机数:
另一种方法是使用 `Math.floor` 来取范围从 `min` 到 `max+1` 的随机数:

```js run
*!*
Expand All @@ -63,4 +63,4 @@ values from 2 ... to 2.9999999999 become 2
values from 3 ... to 3.9999999999 become 3
```

所有间隔的长度相同,所以最终能够均匀分配。所有整数出现的概率都相同了
所有间隔的长度相同,从而使最终能够均匀分配
8 changes: 4 additions & 4 deletions 1-js/05-data-types/02-number/9-random-int-min-max/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 2

---

# 从最小到最大的随机整数
# 从 min 到 max 的随机整数

创建一个函数 `randomInteger(minmax)`,该函数从 `min` 到 `max` 生成随机整数,包括 `min` 和 `max` 作为可能值
创建一个函数 `randomInteger(min, max)`,该函数会生成从 `min` 到 `max` 的随机整数,生成的值包括 `min` 和 `max`。

来自间隔 `min..max` 的任何数字必须以相同的概率出现
取值范围在 `min..max` 中的任何数字出现的概率必须相同


功能示例:
Expand All @@ -17,4 +17,4 @@ alert( random(1, 5) ); // 3
alert( random(1, 5) ); // 5
```

您可以使用[上一个任务](info:task/random-min-max)的解决方案作为基础。
你可以使用 [上一个任务](info:task/random-min-max) 的解决方案作为基础。
Loading