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
Changes from 14 commits
44d479d
2347f7f
b2b8af8
40ae9b0
8a41bc9
559523f
c35b470
51dc110
ab8c478
96d5953
b7b9a00
c1175fa
c2d423f
08cd586
4fa759b
8537a8b
b7a1ef8
0c50d9a
8da7e29
2876487
c34a427
a4c8ee9
731c46f
e2f6020
a3805e6
cfa1fa0
3c63928
cf60589
ae62dda
cc6380d
4557c18
b30a15b
38814e1
3ae94de
4789bae
8dc23fd
6378e59
3aad05a
3ff7839
ce8d114
ffbe6ad
5b1e2da
72d9a12
d691a8b
3b9db8e
777fb3d
38390d4
fc8b6cc
b609fac
00425e2
da20677
fdeb9aa
9bd0ff9
79f51dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
importance: 5 | ||
重要性: 5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
提示:类型会有一个问题。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`内部,小数部分是一个无限的二进制。在这种情况下,它存储的时候有一个精度的缺失。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 数字与汉字之间需要一个空格。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 精度缺失我觉得跟原文有点差别,但损失也有点不太恰当,可以讨论下 |
||
Let's see: | ||
让我们来看看: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
精度缺失可能会导致数字的增加和减少。在这种特殊情况下,数字可能会变少了一点,这就是为什么它减少了。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 精度缺失可能会导致数字的增加和减小。在这种特殊情况下,数字可能会变小了一点,这就是为什么它减小了。 |
||
|
||
And what's for `1.35`? | ||
那么 `1.35` 是怎样样的呢? | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 那么 |
||
```js run | ||
alert( 1.35.toFixed(20) ); // 1.35000000000000008882 | ||
``` | ||
|
||
Here the precision loss made the number a little bit greater, so it rounded up. | ||
在这里,精确度的损失使得这个数字更大一些,所以这个数字变了。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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为例来解决这个问题?** | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 数字与汉字之间需要一个空格。 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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的幂的分数在二进制系统中被精确地表示,现在我们可以围绕它来解决问题: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 数字与汉字之间需要一个空格。 |
||
|
||
```js run | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
importance: 4 | ||
重要性: 4 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`会前进一位。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 数字与汉字之间需要一个空格。 |
||
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? | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 数字与汉字之间需要一个空格。 |
||
```js run | ||
alert( 6.35.toFixed(1) ); // 6.3 | ||
``` | ||
|
||
How to round `6.35` the right way? | ||
如何以正确的方式来四舍五入“6.35”? | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如何以正确的方式来四舍五入 “6.35”? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 数字和汉字之间空格 |
||
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。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在我们停止之后,我们需要专门处理空行和空行(返回 null),因为将它们转换为数字将返回 0。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
importance: 5 | ||
重要性: 5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`,它提示输入一个数字,直到访问者输入一个有效的数字值。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 创建一个函数 |
||
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`。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 访问者也可以通过输入空行或按“取消”来中止该过程。 |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 英文字母和汉字之间空一格 |
||
[demo] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
That's because `i` would never equal `10`. | ||
那是因为 i 永远不会等于10。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 那是因为 i 永远不会等于 10。 |
||
Run it to see the *real* values of `i`: | ||
运行它以查看i的实际值: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 运行它以查看 i 的实际值: |
||
```js run | ||
let i = 0; | ||
|
@@ -10,8 +10,8 @@ while (i < 11) { | |
} | ||
``` | ||
|
||
None of them is exactly `10`. | ||
他们中没有一个完全是10 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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这样的分数时出现了精度损失。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 发生这样的事情是因为在添加像 0.2 这样的分数时出现了精度损失。 |
||
Conclusion: evade equality checks when working with decimal fractions. | ||
结论:在处理小数部分时避免相等检查。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
importance: 4 | ||
重要性: 4 | ||
|
||
--- | ||
|
||
# An occasional infinite loop | ||
#一个偶然出现的无限循环 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一个偶发的无限循环 |
||
|
||
This loop is infinite. It never ends. Why? | ||
这个循环是无限的。它永远不会结束。为什么? | ||
|
||
```js | ||
let i = 0; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
We need to "map" all values from the interval 0..1 into values from `min` to `max`. | ||
我们需要将区间0..1中的所有值“映射”为从最小值到最大值的值。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 现在,如果我们添加`最小值`,则可能的间隔将从最小值变为`最大值`。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果我们将 0..1 的随机数乘以 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 现在,如果我们添加 |
||
|
||
The function: | ||
函数实现: | ||
|
||
```js run | ||
function random(min, max) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Math.random() 用英文的括号。 |
||
Write the function `random(min, max)` to generate a random floating-point number from `min` to `max` (not including `max`). | ||
编写随机函数(min,max)以生成从最小到最大(不包括最大值)的随机浮点数。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`生成一个值并取它四舍五入的值: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 最简单但错误的解决方案是从 |
||
```js run | ||
function randomInteger(min, max) { | ||
|
@@ -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`的概率是其他值的两倍 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 字母和汉字之间要有空格,结尾少了结束句号。 |
||
If you run the example above many times, you would easily see that `2` appears the most often. | ||
如果你多次运行这个例子,你会很容易看到`2`出现的频率最高。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果你多次运行这个例子,你会很容易看到 |
||
That happens because `Math.round()` gets random numbers from the interval `1..3` and rounds them as follows: | ||
发生这种情况是因为`Math.round()`从间隔`1..3`获得随机数并按如下所示进行四舍五入: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
```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`一样。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 数字与汉字之间需要括号。 |
||
# 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的值,从而将所需的概率添加到边缘: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这项任务有很多正确的解决方案。其中之一是调整间隔边界。为了确保相同的时间间隔,我们可以生成从 0.5 到 3.5 的值,从而将所需的概率添加到边缘: |
||
```js run | ||
*!* | ||
|
@@ -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`的随机数: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 另一种方法是使用 |
||
```js run | ||
*!* | ||
|
@@ -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. | ||
所有间隔的长度相同,所以最终能够均匀分配。所有整数出现的概率都相同了 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 结尾少了个句号。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
创建一个函数`randomInteger(min,max)`,该函数从`min`到`max`生成随机整数,包括`min`和`max`作为可能值。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 创建一个函数 |
||
|
||
Any number from the interval `min..max` must appear with the same probability. | ||
来自间隔`min..max`的任何数字必须以相同的概率出现。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
Examples of its work: | ||
功能示例: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)的解决方案作为基础。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (info:task/random-min-max)应该为英文括号。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
否则,
a
和b
会是字符串,它们的总和就是它们的连接,即:“1” + “2” = “12”
。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
校对者说得对,这里需要空格的