Skip to content

Commit

Permalink
1-js/05-data-types/02-number (#114)
Browse files Browse the repository at this point in the history
* translate 1-sum-interface/solution.md

* translate 1-sum-interface.task.md

* translate 2-why-rounder-down task.md

* translate 2

* translate 3-repeat-until-number/task.md

* translate 3-repeat-until-number solution.md

* translate 4-endless-loop-error

* translate -random-min-max

* translate  0-random-int-min-max

* translate article.md to 334

* finish translate wait  proofreading

* update  symbol

* update some not perfect translate

* Update solution.md

* Update solution.md

* Update task.md

* Update solution.md

* Update solution.md

* Update task.md

* Update solution.md

* Update solution.md

* Update task.md

* Update solution.md

* Update solution.md

* Update task.md

* Update task.md

* Update solution.md

* update

* Update task.md

* Update task.md

* change space for num & word

* change half  punctutions to full

* update

* update

* Update solution.md

* Update solution.md

* Update task.md

* Update solution.md

* Update task.md

* Update solution.md

* Update solution.md

* Update solution.md

* Update article.md

* Update article.md

* Update solution.md

* Update task.md

* Update solution.md

* Update solution.md

* Update solution.md

* Update article.md

* Update article.md
  • Loading branch information
lwjcjmx123 authored and leviding committed Jun 13, 2018
1 parent 2dc4c95 commit a09b860
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 193 deletions.
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”`
6 changes: 3 additions & 3 deletions 1-js/05-data-types/02-number/1-sum-interface/task.md
Expand Up @@ -2,10 +2,10 @@ importance: 5

---

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

Let's see:
让我们来看看:

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

And what's for `1.35`?
那么 `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.
在这里,精度缺失使得这个数字更大一些,所以这个数字变大了一些。

**How can we fix the problem with `6.35` if we want it to be rounded the right way?**
**如果我们希望以正确的方式四舍五入,我们如何使用 `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的幂的分数在二进制系统中被精确地表示,现在我们可以围绕它来解决问题:


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

11 changes: 5 additions & 6 deletions 1-js/05-data-types/02-number/2-why-rounded-down/task.md
Expand Up @@ -2,21 +2,20 @@ importance: 4

---

# 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` 会前进一位。

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`

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

How to round `6.35` the right way?

如何以正确的方式来四舍五入 `6.35`
Expand Up @@ -15,9 +15,8 @@ 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`.

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`

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

---

# 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`,它提示输入一个数字,直到访问者输入一个有效的数字。

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`

[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`

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

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

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

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

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

---

# An occasional infinite loop
#一个偶发的无限循环

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

```js
let i = 0;
Expand Down
11 changes: 5 additions & 6 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 中的所有值“映射”为从最小值到最大值。

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. 现在,如果我们添加 `min`,则可能的间隔将从 `min` 变为 `max`

The function:
函数实现:

```js run
function random(min, max) {
Expand All @@ -16,4 +16,3 @@ alert( random(1, 5) );
alert( random(1, 5) );
alert( random(1, 5) );
```

8 changes: 4 additions & 4 deletions 1-js/05-data-types/02-number/8-random-min-max/task.md
Expand Up @@ -2,13 +2,13 @@ importance: 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 )。

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

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` 生成一个值并取它四舍五入的值:

```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` 的概率是其他值的两倍。

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

That happens because `Math.round()` gets random numbers from the interval `1..3` and rounds them as follows:
发生这种情况是因为 `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` 一样。

# 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 的值,从而将所需的概率添加到边缘:

```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` 的随机数:

```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.
所有间隔的长度相同,所以最终能够均匀分配。所有整数出现的概率都相同了。
10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/9-random-int-min-max/task.md
Expand Up @@ -2,19 +2,19 @@ importance: 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.
创建一个函数 `randomInteger(minmax)`,该函数从 `min` `max` 生成随机整数,包括 `min` `max` 作为可能值。

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


Examples of its work:
功能示例:

```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)的解决方案作为基础。

0 comments on commit a09b860

Please sign in to comment.