Skip to content
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

1-js/05-data-types/02-number #114

Merged
merged 54 commits into from Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
44d479d
translate 1-sum-interface/solution.md
lwjcjmx123 Jun 4, 2018
2347f7f
translate 1-sum-interface.task.md
lwjcjmx123 Jun 4, 2018
b2b8af8
translate 2-why-rounder-down task.md
lwjcjmx123 Jun 4, 2018
40ae9b0
translate 2
lwjcjmx123 Jun 4, 2018
8a41bc9
translate 3-repeat-until-number/task.md
lwjcjmx123 Jun 4, 2018
559523f
translate 3-repeat-until-number solution.md
Jun 4, 2018
c35b470
translate 4-endless-loop-error
Jun 4, 2018
51dc110
translate -random-min-max
Jun 4, 2018
ab8c478
translate 0-random-int-min-max
Jun 4, 2018
96d5953
translate article.md to 334
Jun 5, 2018
b7b9a00
finish translate wait proofreading
Jun 6, 2018
c1175fa
update symbol
lwjcjmx123 Jun 6, 2018
c2d423f
update some not perfect translate
Jun 6, 2018
08cd586
Merge branch 'translate/1-js/05-data-types/02-number' of github.com:l…
Jun 6, 2018
4fa759b
Update solution.md
lwjcjmx123 Jun 6, 2018
8537a8b
Update solution.md
lwjcjmx123 Jun 6, 2018
b7a1ef8
Update task.md
lwjcjmx123 Jun 6, 2018
0c50d9a
Update solution.md
lwjcjmx123 Jun 6, 2018
8da7e29
Update solution.md
lwjcjmx123 Jun 6, 2018
2876487
Update task.md
lwjcjmx123 Jun 6, 2018
c34a427
Update solution.md
lwjcjmx123 Jun 6, 2018
a4c8ee9
Update solution.md
lwjcjmx123 Jun 6, 2018
731c46f
Update task.md
lwjcjmx123 Jun 6, 2018
e2f6020
Update solution.md
lwjcjmx123 Jun 6, 2018
a3805e6
Update solution.md
lwjcjmx123 Jun 6, 2018
cfa1fa0
Update task.md
lwjcjmx123 Jun 6, 2018
3c63928
Update task.md
lwjcjmx123 Jun 6, 2018
cf60589
Update solution.md
lwjcjmx123 Jun 7, 2018
ae62dda
update
Jun 7, 2018
cc6380d
Update task.md
leviding Jun 8, 2018
4557c18
Merge branch 'translate/1-js/05-data-types/02-number' of github.com:l…
Jun 8, 2018
b30a15b
Update task.md
lwjcjmx123 Jun 8, 2018
38814e1
change space for num & word
Jun 8, 2018
3ae94de
Merge branch 'translate/1-js/05-data-types/02-number' of github.com:l…
Jun 8, 2018
4789bae
change half punctutions to full
Jun 8, 2018
8dc23fd
update
Jun 8, 2018
6378e59
update
Jun 8, 2018
3aad05a
Update solution.md
leviding Jun 8, 2018
3ff7839
Update solution.md
leviding Jun 8, 2018
ce8d114
Update task.md
leviding Jun 8, 2018
ffbe6ad
Update solution.md
leviding Jun 8, 2018
5b1e2da
Update task.md
leviding Jun 8, 2018
72d9a12
Update solution.md
leviding Jun 8, 2018
d691a8b
Update solution.md
leviding Jun 8, 2018
3b9db8e
Update solution.md
leviding Jun 8, 2018
777fb3d
Update article.md
leviding Jun 8, 2018
38390d4
Update article.md
lwjcjmx123 Jun 12, 2018
fc8b6cc
Update solution.md
leviding Jun 12, 2018
b609fac
Update task.md
leviding Jun 12, 2018
00425e2
Update solution.md
leviding Jun 12, 2018
da20677
Update solution.md
leviding Jun 12, 2018
fdeb9aa
Update solution.md
leviding Jun 12, 2018
9bd0ff9
Update article.md
leviding Jun 13, 2018
79f51dd
Update article.md
leviding Jun 13, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions 1-js/05-data-types/02-number/1-sum-interface/solution.md
Expand Up @@ -7,6 +7,6 @@ let b = +prompt("The second number?", "");
alert( a + b );
```

Note the unary plus `+` before `prompt`. It immediately converts the value to a number.
注意一元符号 `+` `prompt` 前面. 它会把获取的值转换成数字.

Otherwise, `a` and `b` would be string their sum would be their concatenation, that is: `"1" + "2" = "12"`.
否则,`a``b`会是字符串,它们的总和就是它们的连接,即:`“1”+“2”=“12”`。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

否则,ab 会是字符串,它们的总和就是它们的连接,即:“1” + “2” = “12”

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

校对者说得对,这里需要空格的

8 changes: 4 additions & 4 deletions 1-js/05-data-types/02-number/1-sum-interface/task.md
@@ -1,11 +1,11 @@
importance: 5
重要性: 5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句不翻译


---

# Sum numbers from the visitor
# 来自访客的总数

Create a script that prompts the visitor to enter two numbers and then shows their sum.
创建一个脚本,提示访问者输入两个数字,然后显示它们的总和。

[demo]

P.S. There is a gotcha with types.
提示:类型会有一个问题。
16 changes: 8 additions & 8 deletions 1-js/05-data-types/02-number/2-why-rounded-down/solution.md
@@ -1,30 +1,30 @@
Internally the decimal fraction `6.35` is an endless binary. As always in such cases, it is stored with a precision loss.
`6.35`内部,小数部分是一个无限的二进制。在这种情况下,它存储的时候有一个精度的缺失。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数字与汉字之间需要一个空格。
6.35 内部,小数部分是一个无限的二进制。在这种情况下,它存储的时候存在精度的损失。
空格问题有点多,请仔细阅读译文要求

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

精度缺失我觉得跟原文有点差别,但损失也有点不太恰当,可以讨论下

Let's see:
让我们来看看:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

标点用中文的


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

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.
精度缺失可能会导致数字的增加和减少。在这种特殊情况下,数字可能会变少了一点,这就是为什么它减少了。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


And what's for `1.35`?
那么 `1.35` 是怎样样的呢?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那么 1.35 是怎样的呢?

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

Here the precision loss made the number a little bit greater, so it rounded up.
在这里,精确度的损失使得这个数字更大一些,所以这个数字变了。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the precision loss 翻译应该与上文保持一致。
所以这个数字被五入了。


**How can we fix the problem with `6.35` if we want it to be rounded the right way?**
**如果我们希望以正确的方式四舍五入,我们如何使用6.35为例来解决这个问题?**

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数字与汉字之间需要一个空格。
如果我们希望以正确的方式四舍五入,我们如何使用 6.35 为例来解决这个问题?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

We should bring it closer to an integer prior to rounding:
在四舍五入之前,我们应该把它接近整数:

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

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:
请注意`63.5`完全没有精度损失。这是因为小数部分`0.5`实际上是`1 / 2`。除以2的幂的分数在二进制系统中被精确地表示,现在我们可以围绕它来解决问题:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数字与汉字之间需要一个空格。
请注意 63.5 完全没有精度损失。这是因为小数部分 0.5 实际上是 1/2。除以 2 的幂的分数在二进制系统中可以精确地表示,现在我们可以围绕它来解决问题:


```js run
Expand Down
12 changes: 6 additions & 6 deletions 1-js/05-data-types/02-number/2-why-rounded-down/task.md
@@ -1,22 +1,22 @@
importance: 4
重要性: 4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本句不翻译,保留原文


---

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

According to the documentation `Math.round` and `toFixed` both round to the nearest number: `0..4` lead down while `5..9` lead up.
根据文档`Math.round``toFixed`,最近的数字四舍五入:`0..4`会被舍去,而`5..9`会前进一位。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数字与汉字之间需要一个空格。
根据文档 Math.roundtoFixed,最近的数字四舍五入:0..4 会被舍去,而 5..9 会前进一位。

For instance:
例如:

```js run
alert( 1.35.toFixed(1) ); // 1.4
```

In the similar example below, why is `6.35` rounded to `6.3`, not `6.4`?
在下面的类似例子中,为什么6.35被四舍五入为6.3而不是6.4?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数字与汉字之间需要一个空格。
在下面的类似例子中,为什么 6.35 被四舍五入为 6.3 而不是 6.4?

```js run
alert( 6.35.toFixed(1) ); // 6.3
```

How to round `6.35` the right way?
如何以正确的方式来四舍五入“6.35”?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如何以正确的方式来四舍五入 “6.35”?

Expand Up @@ -15,9 +15,9 @@ function readNumber() {
alert(`Read: ${readNumber()}`);
```

The solution is a little bit more intricate that it could be because we need to handle `null`/empty lines.
该解决方案有点复杂,可能是因为我们需要处理 null/空行。

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`.
所以我们实际上接受输入,直到它成为“常规数字”。 null(取消)和空行都适合该条件,因为在数字形式中它们是0。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

After we stopped, we need to treat `null` and empty line specially (return `null`), because converting them to a number would return `0`.
在我们停止之后,我们需要专门处理空行和空行(返回null),因为将它们转换为数字将返回0。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在我们停止之后,我们需要专门处理空行和空行(返回 null),因为将它们转换为数字将返回 0。

10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/3-repeat-until-number/task.md
@@ -1,14 +1,14 @@
importance: 5
重要性: 5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不翻译


---

# Repeat until the input is a number
# 重复检测,直到输入是一个数字

Create a function `readNumber` which prompts for a number until the visitor enters a valid numeric value.
创建一个函数`readNumber`,它提示输入一个数字,直到访问者输入一个有效的数字值。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

The resulting value must be returned as a number.
结果值必须作为数字返回。

The visitor can also stop the process by entering an empty line or pressing "CANCEL". In that case, the function should return `null`.
访问者也可以通过输入空行或按“取消”来停止该过程。在这种情况下,函数应该返回`null`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

访问者也可以通过输入空行或按“取消”来中止该过程。


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

英文字母和汉字之间空一格
访问者也可以通过输入空行或按“取消”来停止该过程。在这种情况下,函数应该返回 null

[demo]

10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/4-endless-loop-error/solution.md
@@ -1,6 +1,6 @@
That's because `i` would never equal `10`.
那是因为 i 永远不会等于10。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那是因为 i 永远不会等于 10。

Run it to see the *real* values of `i`:
运行它以查看i的实际值:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

运行它以查看 i 的实际值:

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

None of them is exactly `10`.
他们中没有一个完全是10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Such things happen because of the precision losses when adding fractions like `0.2`.
发生这样的事情是因为在添加像0.2这样的分数时出现了精度损失。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Conclusion: evade equality checks when working with decimal fractions.
结论:在处理小数部分时避免相等检查。
6 changes: 3 additions & 3 deletions 1-js/05-data-types/02-number/4-endless-loop-error/task.md
@@ -1,10 +1,10 @@
importance: 4
重要性: 4

---

# An occasional infinite loop
#一个偶然出现的无限循环

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个偶发的无限循环


This loop is infinite. It never ends. Why?
这个循环是无限的。它永远不会结束。为什么?

```js
let i = 0;
Expand Down
10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/8-random-min-max/solution.md
@@ -1,11 +1,11 @@
We need to "map" all values from the interval 0..1 into values from `min` to `max`.
我们需要将区间0..1中的所有值“映射”为从最小值到最大值的值。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们需要将区间 0..1 中的所有值“映射”为从最小值到最大值。

That can be done in two stages:
这可以分两个阶段完成:

1. If we multiply a random number from 0..1 by `max-min`, then it the interval of possible values increases `0..1` to `0..max-min`.
2. Now if we add `min`, the possible interval becomes from `min` to `max`.
1. 如果我们将0..1的随机数乘以`max-min`,则可能值的间隔从0..1增加到0..`max-min`
2. 现在,如果我们添加`最小值`,则可能的间隔将从最小值变为`最大值`。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果我们将 0..1 的随机数乘以 max-min,则可能值的间隔从 0..1 增加到 0.. max-min

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在,如果我们添加最小值,则可能的间隔将从最小值变为最大值


The function:
函数实现:

```js run
function random(min, max) {
Expand Down
10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/8-random-min-max/task.md
@@ -1,14 +1,14 @@
importance: 2
重要性: 2

---

# A random number from min to max
# 从最小到最大的随机数

The built-in function `Math.random()` creates a random value from `0` to `1` (not including `1`).
用内置函数Math.random()创建一个从0到1的随机值(不包括1)。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math.random() 用英文的括号。
用内置函数 Math.random() 创建一个从 0 到 1 的随机值(不包括 1)。

Write the function `random(min, max)` to generate a random floating-point number from `min` to `max` (not including `max`).
编写随机函数(minmax)以生成从最小到最大(不包括最大值)的随机浮点数。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Examples of its work:
实例:

```js
alert( random(1, 5) ); // 1.2345623452
Expand Down
22 changes: 11 additions & 11 deletions 1-js/05-data-types/02-number/9-random-int-min-max/solution.md
@@ -1,6 +1,6 @@
# The simple but wrong solution
# 简单但错误的解决方案

The simplest, but wrong solution would be to generate a value from `min` to `max` and round it:
最简单但错误的解决方案是从`min``max`生成一个值并取它四舍五入的值:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最简单但错误的解决方案是从 minmax 生成一个值并取它四舍五入的值:

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

The function works, but it is incorrect. The probability to get edge values `min` and `max` is two times less than any other.
这个函数是能起作用的,但不正确。获得边缘值`min``max`的概率是其他值的两倍

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

字母和汉字之间要有空格,结尾少了结束句号。
这个函数是能起作用的,但不正确。获得边缘值 minmax 的概率是其他值的两倍。

If you run the example above many times, you would easily see that `2` appears the most often.
如果你多次运行这个例子,你会很容易看到`2`出现的频率最高。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

That happens because `Math.round()` gets random numbers from the interval `1..3` and rounds them as follows:
发生这种情况是因为`Math.round()`从间隔`1..3`获得随机数并按如下所示进行四舍五入:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math.round() 要用英文括号。
发生这种情况是因为 Math.round() 从间隔 1..3 获得随机数并按如下所示进行四舍五入:

```js no-beautify
values from 1 ... to 1.4999999999 become 1
values from 1.5 ... to 2.4999999999 become 2
values from 2.5 ... to 2.9999999999 become 3
```

Now we can clearly see that `1` gets twice less values than `2`. And the same with `3`.
现在我们可以清楚地看到`1`的值比`2`少两倍。和`3`一样。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数字与汉字之间需要括号。
现在我们可以清楚地看到 1 的值比 2 的值少了两倍。和 3 一样。

# The correct solution
# 正确的解决方案

There are many correct solutions to the task. One of them is to adjust interval borders. To ensure the same intervals, we can generate values from `0.5 to 3.5`, thus adding the required probabilities to the edges:
这项任务有很多正确的解决方案。其中之一是调整间隔边界。为了确保相同的时间间隔,我们可以生成从0.5到3.5的值,从而将所需的概率添加到边缘:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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

An alternative way could be to use `Math.floor` for a random number from `min` to `max+1`:
另一种方法是使用`Math.floor`作为从`min``max + 1`的随机数:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另一种方法是使用 Math.floor 生成从 minmax + 1 的随机数:

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

Now all intervals are mapped this way:
现在所有间隔都以这种方式映射:

```js no-beautify
values from 1 ... to 1.9999999999 become 1
values from 2 ... to 2.9999999999 become 2
values from 3 ... to 3.9999999999 become 3
```

All intervals have the same length, making the final distribution uniform.
所有间隔的长度相同,所以最终能够均匀分配。所有整数出现的概率都相同了

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

结尾少了个句号。
所有间隔的长度相同,所以最终能够均匀分配。
最后一句翻译多余了。

12 changes: 6 additions & 6 deletions 1-js/05-data-types/02-number/9-random-int-min-max/task.md
@@ -1,20 +1,20 @@
importance: 2
重要性: 2

---

# A random integer from min to max
#从最小到最大的随机整数

Create a function `randomInteger(min, max)` that generates a random *integer* number from `min` to `max` including both `min` and `max` as possible values.
创建一个函数`randomIntegerminmax)`,该函数从`min``max`生成随机整数,包括`min``max`作为可能值。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Any number from the interval `min..max` must appear with the same probability.
来自间隔`min..max`的任何数字必须以相同的概率出现。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min..max之间 的任何数字必须以相同的概率出现。


Examples of its work:
功能示例:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

与上文保持一致


```js
alert( random(1, 5) ); // 1
alert( random(1, 5) ); // 3
alert( random(1, 5) ); // 5
```

You can use the solution of the [previous task](info:task/random-min-max) as the base.
您可以使用[上一个任务](info:task/random-min-max)的解决方案作为基础。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(info:task/random-min-max)应该为英文括号。
您可以使用上一个任务的解决方案作为参考。