Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
5295976
Update article.md
EmilyQiRabbit Apr 23, 2018
ff5d85f
Update task.md
EmilyQiRabbit Apr 23, 2018
1e53858
Update solution.md
EmilyQiRabbit Apr 23, 2018
ba9c041
Update solution.md
EmilyQiRabbit Apr 23, 2018
953d9f7
Update solution.md
EmilyQiRabbit Apr 23, 2018
c02aad8
Update task.md
EmilyQiRabbit Apr 23, 2018
58bb3c8
Update task.md
EmilyQiRabbit Apr 23, 2018
f7fcbc3
Update task.md
EmilyQiRabbit Apr 23, 2018
759ef6f
Update solution.md
EmilyQiRabbit Apr 23, 2018
a124e26
Update task.md
EmilyQiRabbit Apr 23, 2018
6e70422
Update task.md
EmilyQiRabbit Apr 23, 2018
2b064fc
Update task.md
EmilyQiRabbit Apr 23, 2018
5ff4fbf
Update solution.md
EmilyQiRabbit Apr 23, 2018
7bd38b0
Update task.md
EmilyQiRabbit Apr 23, 2018
8b7dd23
Update task.md
EmilyQiRabbit Apr 23, 2018
26e8909
Update solution.md
EmilyQiRabbit Apr 23, 2018
87511ea
Update solution.md
EmilyQiRabbit Apr 23, 2018
969afac
Update task.md
EmilyQiRabbit Apr 23, 2018
f1628cb
Update task.md
EmilyQiRabbit Apr 23, 2018
51bc64d
Update task.md
EmilyQiRabbit Apr 23, 2018
e1c3824
Update task.md
EmilyQiRabbit Apr 23, 2018
6a30d26
Update solution.md
EmilyQiRabbit Apr 23, 2018
dce6511
Update task.md
EmilyQiRabbit Apr 23, 2018
30c67a8
Update solution.md
EmilyQiRabbit Apr 23, 2018
d04ee4e
Update solution.md
EmilyQiRabbit Apr 23, 2018
b33b019
Update task.md
EmilyQiRabbit Apr 23, 2018
c656e5d
Update article.md
EmilyQiRabbit Apr 23, 2018
e0a0a3e
Update task.md
EmilyQiRabbit Apr 23, 2018
d4cd396
Update task.md
EmilyQiRabbit Apr 23, 2018
688b198
Update task.md
EmilyQiRabbit Apr 23, 2018
1826831
Update task.md
EmilyQiRabbit Apr 23, 2018
d1b13f5
Update task.md
EmilyQiRabbit Apr 23, 2018
95adb85
Update task.md
EmilyQiRabbit Apr 23, 2018
00b7deb
Update task.md
EmilyQiRabbit Apr 23, 2018
80229cb
Update task.md
EmilyQiRabbit Apr 23, 2018
3debe14
Update task.md
EmilyQiRabbit Apr 23, 2018
f0636a9
Update solution.md
EmilyQiRabbit Apr 23, 2018
1922597
Update solution.md
EmilyQiRabbit Apr 23, 2018
cbfca76
Update task.md
EmilyQiRabbit Apr 23, 2018
c91b18e
Update solution.md
leviding Apr 23, 2018
b6a0f2a
Update article.md
leviding Apr 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer is `2`, that's the first truthy value.
结果是 `2`,这是第一个真值。

```js run
alert( null || 2 || undefined );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# What's the result of OR?
# 或运算的结果是什么?

What the code below is going to output?
如下代码将会输出什么?

```js
alert( null || 2 || undefined );
Expand Down
12 changes: 6 additions & 6 deletions 1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
The answer: first `1`, then `2`.
答案:首先是 `1`,然后是 `2`

```js run
alert( alert(1) || 2 || alert(3) );
```

The call to `alert` does not return a value. Or, in other words, it returns `undefined`.
`alert` 的调用没有返回值。或者说返回的是 `undefined`

1. The first OR `||` evaluates it's left operand `alert(1)`. That shows the first message with `1`.
2. The `alert` returns `undefined`, so OR goes on to the second operand searching for a truthy value.
3. The second operand `2` is truthy, so the execution is halted, `2` is returned and then shown by the outer alert.
1. 第一个或运算 `||` 对它的左值 `alert(1)` 进行了计算。这就显示了第一条 `1` 的信息。
2. 函数 `alert` 返回了 `undefined`,所以或运算继续检查第二个操作数,寻找真值。
3. 第二个操作数 `2` 是真值,所以执行就中断了。`2` 被返回,并且被外层的 alert 显示。

There will be no `3`, because the evaluation does not reach `alert(3)`.
不会显示 `3`,因为运算没有抵达 `alert(3)`
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/11-logical-operators/2-alert-or/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# What's the result of OR'ed alerts?
# 或运算和 alerts 的结果是什么?

What the code below will output?
下面的代码将会输出什么?

```js
alert( alert(1) || 2 || alert(3) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer: `null`, because it's the first falsy value from the list.
答案:`null`,因为它是列表中第一个假值。

```js run
alert( 1 && null && 2 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# What is the result of AND?
# 与操作的结果是什么?

What this code is going to show?
下面这段代码将会显示什么?

```js
alert( 1 && null && 2 );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
The answer: `1`, and then `undefined`.
答案:`1`,然后 `undefined`

```js run
alert( alert(1) && alert(2) );
```

The call to `alert` returns `undefined` (it just shows a message, so there's no meaningful return).
调用 `alert` 返回了 `undefined`(它只展示消息,所以没有有意义的返回值)。

Because of that, `&&` evaluates the left operand (outputs `1`), and immediately stops, because `undefined` is a falsy value. And `&&` looks for a falsy value and returns it, so it's done.
因此,`&&` 计算了它左边的操作数(显示 `1`),然后立即停止了,因为 `undefined` 是一个假值。并且 `&&` 就是寻找假值然后返回它,所以运算结束。

4 changes: 2 additions & 2 deletions 1-js/02-first-steps/11-logical-operators/4-alert-and/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# What is the result of AND'ed alerts?
# 与运算连接的 alerts 的结果是什么?

What will this code show?
这段代码将会显示什么?

```js
alert( alert(1) && alert(2) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
The answer: `3`.
答案:`3`

```js run
alert( null || 2 && 3 || 4 );
```

The precedence of AND `&&` is higher than `||`, so it executes first.
与运算 `&&` 的优先级比 `||` 高,所以它第一个被执行。

The result of `2 && 3 = 3`, so the expression becomes:
结果是 `2 && 3 = 3`,所以表达式变成了:

```
null || 3 || 4
```

Now the result if the first truthy value: `3`.
现在的结果就是第一个真值:`3`

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# The result of OR AND OR
# 或运算、与运算、或运算串联的结果

What will be the result?
结果将会是什么?

```js
alert( null || 2 && 3 || 4 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ importance: 3

---

# Check the range between
# 检查值是否位于范围区间内

Write an "if" condition to check that `age` is between `14` and `90` inclusively.
写一个“if”条件句来检查 `age` 是否位于 `14` 到 `90` 的闭区间。

"Inclusively" means that `age` can reach the edges `14` or `90`.
“闭区间”意味着,`age` 的值可以取 `14` `90`
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
The first variant:
第一个表达式:

```js
if (!(age >= 14 && age <= 90))
```

The second variant:
第二个表达式:

```js
if (age < 14 || age > 90)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ importance: 3

---

# Check the range outside
# 检测值是否位于范围之外

Write an `if` condition to check that `age` is NOT between 14 and 90 inclusively.
写一个 `if` 条件句,检查 `age` 是否不位于 14 90 的闭区间。

Create two variants: the first one using NOT `!`, the second one -- without it.
创建两个表达式:第一个用非运算 `!`,第二个不用。
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
The answer: the first and the third will execute.
答案:第一个和第三个将会被执行。

Details:
详解:

```js run
// Runs.
// The result of -1 || 0 = -1, truthy
// 执行。
// -1 || 0 的结果为 -1,真值
if (-1 || 0) alert( 'first' );

// Doesn't run
// -1 && 0 = 0, falsy
// 不执行。
// -1 && 0 = 0,假值
if (-1 && 0) alert( 'second' );

// Executes
// Operator && has a higher precedence than ||
// so -1 && 1 executes first, giving us the chain:
// 执行
// && 运算的优先级比 || 高
// 所以 -1 && 1 先执行,给出如下运算链:
// null || -1 && 1 -> null || 1 -> 1
if (null || -1 && 1) alert( 'third' );
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# A question about "if"
# 一个关于 "if" 的问题

Which of these `alert`s are going to execute?
下面哪一个 `alert` 将会被执行?

What will be the results of the expressions inside `if(...)`?
`if(...)` 语句内表达式的结果是什么?

```js
if (-1 || 0) alert( 'first' );
Expand Down
Loading