Skip to content

Commit 9767d68

Browse files
authored
pr764-2 1-js/02-first-steps/08-operators/4-fix-prompt/ (#775)
pr764-2 1-js/02-first-steps/08-operators/4-fix-prompt/ (#775)
1 parent 66db9b9 commit 9767d68

File tree

3 files changed

+68
-68
lines changed

3 files changed

+68
-68
lines changed
+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
The reason is that prompt returns user input as a string.
1+
原因是 prompt 以字符串的形式返回用户的输入。
22

3-
So variables have values `"1"` and `"2"` respectively.
3+
所以变量的值分别为 `"1"` `"2"`
44

55
```js run
66
let a = "1"; // prompt("First number?", 1);
@@ -9,9 +9,9 @@ let b = "2"; // prompt("Second number?", 2);
99
alert(a + b); // 12
1010
```
1111

12-
What we should to is to convert strings to numbers before `+`. For example, using `Number()` or prepending them with `+`.
12+
我们应该做的是,在 `+` 之前将字符串转换为数字。例如,使用 `Number()` 或在 `prompt` 前加 `+`
1313

14-
For example, right before `prompt`:
14+
例如,就在 `prompt` 之前加 `+`
1515

1616
```js run
1717
let a = +prompt("First number?", 1);
@@ -20,7 +20,7 @@ let b = +prompt("Second number?", 2);
2020
alert(a + b); // 3
2121
```
2222

23-
Or in the `alert`:
23+
或在 `alert` 中:
2424

2525
```js run
2626
let a = prompt("First number?", 1);
@@ -29,4 +29,4 @@ let b = prompt("Second number?", 2);
2929
alert(+a + +b); // 3
3030
```
3131

32-
Using both unary and binary `+` in the latest code. Looks funny, doesn't it?
32+
在最新的代码中,同时使用一元和二元的 `+`。看起来很有趣,不是吗?

Diff for: 1-js/02-first-steps/08-operators/4-fix-prompt/task.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ importance: 5
22

33
---
44

5-
# Fix the addition
5+
# 修正加法
66

7-
Here's a code that asks the user for two numbers and shows their sum.
7+
这里有一段代码,要求用户输入两个数字并显示它们的总和。
88

9-
It works incorrectly. The output in the example below is `12` (for default prompt values).
9+
它的运行结果不正确。下面例子中的输出是 `12`(对于默认的 prompt 的值)。
1010

11-
Why? Fix it. The result should be `3`.
11+
为什么会这样?修正它。结果应该是 `3`
1212

1313
```js run
1414
let a = prompt("First number?", 1);

Diff for: 1-js/02-first-steps/08-operators/article.md

+58-58
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Basic operators, maths
1+
# 基础运算符,数学
22

33
我们从学校里了解到过很多运算符,比如说加号 `+`、乘号 `*`、减号 `-` 等。
44

5-
In this chapter, we’ll start with simple operators, then concentrate on JavaScript-specific aspects, not covered by school arithmetic.
5+
在本章中,我们将从简单的运算符开始,然后着重介绍 JavaScript 特有的方面,这些是在学校中学习的数学所没有涵盖的。
66

77
## 术语:“一元运算符”,“二元运算符”,“运算元”
88

@@ -26,57 +26,57 @@ In this chapter, we’ll start with simple operators, then concentrate on JavaSc
2626
alert( y - x ); // 2,二元运算符减号做减运算
2727
```
2828

29-
严格地说,在上面的示例中,我们使用一个相同的符号表征了两个不同的运算符:负号运算符,即反转符号的一元运算符,减法运算符,是从另一个数减去一个数的二进制运算符
29+
严格地说,在上面的示例中,我们使用一个相同的符号表征了两个不同的运算符:负号运算符,即反转符号的一元运算符,减法运算符,是从另一个数减去一个数的二元运算符
3030

31-
## Maths
31+
## 数学
3232

33-
The following math operations are supported:
33+
支持以下数学运算:
3434

35-
- Addition `+`,
36-
- Subtraction `-`,
37-
- Multiplication `*`,
38-
- Division `/`,
39-
- Remainder `%`,
40-
- Exponentiation `**`.
35+
- 加法 `+`,
36+
- 减法 `-`,
37+
- 乘法 `*`,
38+
- 除法 `/`,
39+
- 取余 `%`,
40+
- 求幂 `**`.
4141

42-
The first four are straightforward, while `%` and `**` need a few words about them.
42+
前四个都很简单,而 `%` `**` 则需要说一说。
4343

44-
### Remainder %
44+
### 取余 %
4545

46-
The remainder operator `%`, despite its appearance, is not related to percents.
46+
取余运算符是 `%`,尽管它看起来很像百分数,但实际并无关联。
4747

48-
The result of `a % b` is the [remainder](https://en.wikipedia.org/wiki/Remainder) of the integer division of `a` by `b`.
48+
`a % b` 的结果是 `a` 整除 `b` 的 [余数](https://zh.wikipedia.org/zh-hans/%E4%BD%99%E6%95%B0))。
4949

50-
For instance:
50+
例如:
5151

5252
```js run
53-
alert( 5 % 2 ); // 1, a remainder of 5 divided by 2
54-
alert( 8 % 3 ); // 2, a remainder of 8 divided by 3
53+
alert( 5 % 2 ); // 1,5 除以 2 的余数
54+
alert( 8 % 3 ); // 2,8 除以 3 的余数
5555
```
5656

57-
### Exponentiation **
57+
### 求幂 **
5858

59-
The exponentiation operator `a ** b` multiplies `a` by itself `b` times.
59+
求幂运算 `a ** b` `a` 乘以自身 `b` 次。
6060

61-
For instance:
61+
例如:
6262

6363
```js run
64-
alert( 2 ** 2 ); // 4 (2 multiplied by itself 2 times)
65-
alert( 2 ** 3 ); // 8 (2 * 2 * 2, 3 times)
66-
alert( 2 ** 4 ); // 16 (2 * 2 * 2 * 2, 4 times)
64+
alert( 2 ** 2 ); // 4 (2 * 2,自乘 2 次)
65+
alert( 2 ** 3 ); // 8 (2 * 2 * 2,自乘 3 )
66+
alert( 2 ** 4 ); // 16 (2 * 2 * 2 * 2,自乘 4 )
6767
```
6868

69-
Mathematically, the exponentiation is defined for non-integer numbers as well. For example, a square root is an exponentiation by `1/2`:
69+
在数学上,求幂的定义也适用于非整数。例如,平方根是以 `1/2` 为单位的求幂:
7070

7171
```js run
72-
alert( 4 ** (1/2) ); // 2 (power of 1/2 is the same as a square root)
73-
alert( 8 ** (1/3) ); // 2 (power of 1/3 is the same as a cubic root)
72+
alert( 4 ** (1/2) ); // 21/2 次方与平方根相同)
73+
alert( 8 ** (1/3) ); // 21/3 次方与立方根相同)
7474
```
7575

7676

77-
## String concatenation with binary +
77+
## 用二元运算符 + 连接字符串
7878

79-
Let's meet features of JavaScript operators that are beyond school arithmetics.
79+
我们来看一些学校算术未涉及的 JavaScript 运算符的特性。
8080

8181
通常,加号 `+` 用于求和。
8282

@@ -96,23 +96,23 @@ alert( '1' + 2 ); // "12"
9696
alert( 2 + '1' ); // "21"
9797
```
9898

99-
See, it doesn't matter whether the first operand is a string or the second one.
99+
你看,第一个运算元和第二个运算元,哪个是字符串并不重要。
100100

101-
Here's a more complex example:
101+
下面是一个更复杂的例子:
102102

103103
```js run
104-
alert(2 + 2 + '1' ); // "41" and not "221"
104+
alert(2 + 2 + '1' ); // "41",不是 "221"
105105
```
106106

107-
Here, operators work one after another. The first `+` sums two numbers, so it returns `4`, then the next `+` adds the string `1` to it, so it's like `4 + '1' = 41`.
107+
在这里,运算符是按顺序工作。第一个 `+` 将两个数字相加,所以返回 `4`,然后下一个 `+` 将字符串 `1` 加入其中,所以就是 `4 + '1' = 41`
108108

109-
The binary `+` is the only operator that supports strings in such a way. Other arithmetic operators work only with numbers and always convert their operands to numbers.
109+
二元 `+` 是唯一一个以这种方式支持字符串的运算符。其他算术运算符只对数字起作用,并且总是将其运算元转换为数字。
110110

111-
Here's the demo for subtraction and division:
111+
下面是减法和除法运算的示例:
112112

113113
```js run
114-
alert( 6 - '2' ); // 4, converts '2' to a number
115-
alert( '6' / '2' ); // 3, converts both operands to numbers
114+
alert( 6 - '2' ); // 4,将 '2' 转换为数字
115+
alert( '6' / '2' ); // 3,将两个运算元都转换为数字
116116
```
117117

118118
## 数字转化,一元运算符 +
@@ -187,7 +187,7 @@ alert( +apples + +oranges ); // 5
187187
| ... | ... | ... |
188188
| 17 | 一元加号 | `+` |
189189
| 17 | 一元负号 | `-` |
190-
| 16 | exponentiation | `**` |
190+
| 16 | 求幂 | `**` |
191191
| 15 | 乘号 | `*` |
192192
| 15 | 除号 | `/` |
193193
| 13 | 加号 | `+` |
@@ -210,11 +210,11 @@ let x = 2 * 2 + 1;
210210
alert( x ); // 5
211211
```
212212

213-
### Assignment = returns a value
213+
### 赋值 = 返回一个值
214214

215-
The fact of `=` being an operator, not a "magical" language construct has an interesting implication.
215+
`=` 是一个运算符,而不是一个有着“魔法”作用的语言结构。
216216

217-
Most operators in JavaScript return a value. That's obvious for `+` and `-`, but also true for `=`.
217+
JavaScript 中,大多数运算符都会返回一个值。这对于 `+` `-` 来说是显而易见的,但对于 `=` 来说也是如此。
218218

219219
语句 `x = value` 将值 `value` 写入 `x` **然后返回 x**
220220

@@ -234,13 +234,13 @@ alert( c ); // 0
234234

235235
上面这个例子,`(a = b + 1)` 的结果是赋给 `a` 的值(也就是 `3`)。然后该值被用于进一步的运算。
236236

237-
Funny code, isn't it? We should understand how it works, because sometimes we see it in JavaScript libraries.
237+
有趣的代码,不是吗?我们应该了解它的工作原理,因为有时我们会在 JavaScript 库中看到它。
238238

239-
Although, please don't write the code like that. Such tricks definitely don't make code clearer or readable.
239+
不过,请不要写这样的代码。这样的技巧绝对不会使代码变得更清晰或可读。
240240

241-
### Chaining assignments
241+
### 链式赋值(Chaining assignments
242242

243-
Another interesting feature is the ability to chain assignments:
243+
另一个有趣的特性是链式赋值:
244244

245245
```js run
246246
let a, b, c;
@@ -254,49 +254,49 @@ alert( b ); // 4
254254
alert( c ); // 4
255255
```
256256

257-
Chained assignments evaluate from right to left. First, the rightmost expression `2 + 2` is evaluated and then assigned to the variables on the left: `c`, `b` and `a`. At the end, all the variables share a single value.
257+
链式赋值从右到左进行计算。首先,对最右边的表达式 `2 + 2` 求值,然后将其赋给左边的变量:`c``b` `a`。最后,所有的变量共享一个值。
258258

259-
Once again, for the purposes of readability it's better to split such code into few lines:
259+
同样,出于可读性,最好将这种代码分成几行:
260260

261261
```js
262262
c = 2 + 2;
263263
b = c;
264264
a = c;
265265
```
266-
That's easier to read, especially when eye-scanning the code fast.
266+
这样可读性更强,尤其是在快速浏览代码的时候。
267267

268-
## Modify-in-place
268+
## 原地修改
269269

270-
We often need to apply an operator to a variable and store the new result in that same variable.
270+
我们经常需要对一个变量做运算,并将新的结果存储在同一个变量中。
271271

272-
For example:
272+
例如:
273273

274274
```js
275275
let n = 2;
276276
n = n + 5;
277277
n = n * 2;
278278
```
279279

280-
This notation can be shortened using the operators `+=` and `*=`:
280+
可以使用运算符 `+=` `*=` 来缩写这种表示。
281281

282282
```js run
283283
let n = 2;
284-
n += 5; // now n = 7 (same as n = n + 5)
285-
n *= 2; // now n = 14 (same as n = n * 2)
284+
n += 5; // 现在 n = 7(等同于 n = n + 5
285+
n *= 2; // 现在 n = 14(等同于 n = n * 2
286286
287287
alert( n ); // 14
288288
```
289289

290-
Short "modify-and-assign" operators exist for all arithmetical and bitwise operators: `/=`, `-=`, etc.
290+
所有算术和位运算符都有简短的“修改并赋值”运算符:`/=``-=` 等。
291291

292-
Such operators have the same precedence as a normal assignment, so they run after most other calculations:
292+
这类运算符的优先级与普通赋值运算符的优先级相同,所以它们在大多数其他运算之后执行:
293293

294294
```js run
295295
let n = 2;
296296
297297
n *= 3 + 5;
298298
299-
alert( n ); // 16 (right part evaluated first, same as n *= 8)
299+
alert( n ); // 16 (右边部分先被计算,等同于 n *= 8
300300
```
301301

302302
## 自增/自减
@@ -428,7 +428,7 @@ counter++;
428428
- 右移 ( `>>` )
429429
- 无符号右移 ( `>>>` )
430430

431-
These operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. We won't need these operators any time soon, as web development has little use of them, but in some special areas, such as cryptography, they are useful. You can read the [Bitwise Operators](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) article on MDN when a need arises.
431+
这些运算符很少被使用,一般是我们需要在最低级别(位)上操作数字时才使用。我们不会很快用到这些运算符,因为在 Web 开发中很少使用它们,但在某些特殊领域中,例如密码学,它们很有用。当你需要了解它们的时候,可以阅读 MDN 上的 [位操作符](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) 一文。
432432

433433
## 逗号运算符
434434

0 commit comments

Comments
 (0)