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

Logical operators #30

Merged
merged 6 commits into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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?
# OR 的結果是?

What is the code below going to output?
以下程式碼會輸出什麼?

```js
alert( null || 2 || undefined );
Expand Down
13 changes: 7 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,14 @@
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. 第一個 OR `||` 核定它左側的運算元 `alert(1)`,它顯示第一段訊息 `1`。
2. 該 `alert` 回傳 `undefined`,接著 OR 來到第二個運算元尋找真值。
3. 第二個運算元 `2` 是真值,所以執行停止,`2` 被回傳並且被外部的 alert 顯示。

不會顯示 `3`,因為核定根本到不了 `alert(3)`。

There will be no `3`, because the evaluation does not reach `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?
# 使用 OR 的 alert 們,結果會是?
lenchen1112 marked this conversation as resolved.
Show resolved Hide resolved

What will the code below 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?
# AND 的結果是?

What is this code 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?
# 使用 AND 的 alert 們,結果會是?
lenchen1112 marked this conversation as resolved.
Show resolved Hide resolved

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.
AND `&&` 的優先權比 `||` 高,所以會先被執行。

The result of `2 && 3 = 3`, so the expression becomes:
其結果為 `2 && 3 = 3`,所以表達式變成:

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

Now the result is 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
# OR AND OR 的結果

What will the result be?
這樣的結果會是?

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


kaddopur marked this conversation as resolved.
Show resolved Hide resolved
```js
if (age >= 14 && age <= 90)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ importance: 3

---

# Check the range between
# 檢查區間

Write an "if" condition to check that `age` is between `14` and `90` inclusively.
寫一個 "if" 條件來檢查 `age` 是否在包含 `14` 至 `90` 的閉區間內。

"包含" 代表著 `age` 可抵達 `14` 或 `90` 邊界。

"Inclusively" means that `age` can reach the edges `14` or `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,9 @@ 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 的閉區間內。

寫出兩種變化:第一個使用 NOT `!`,第二個不使用。

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` 們哪一個將執行?
lenchen1112 marked this conversation as resolved.
Show resolved Hide resolved

What will the results of the expressions be inside `if(...)`?
`if(...)` 內表達式的結果是什麼?

```js
if (-1 || 0) alert( 'first' );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


```js run demo
let userName = prompt("Who's there?", '');

Expand All @@ -22,4 +20,5 @@ if (userName == 'Admin') {
}
```

Note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable.
注意 `if` 區塊中的垂直縮排,技術上雖然不一定需要,但可以讓程式碼更為易讀。

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

---

# Check the login
# 檢查登入

Write the code which asks for a login with `prompt`.
寫段程式碼使用 `prompt` 來要求登入。

If the visitor enters `"Admin"`, then `prompt` for a password, if the input is an empty line or `key:Esc` -- show "Canceled", if it's another string -- then show "I don't know you".
若訪問者輸入 `"Admin"`,則用 `prompt` 詢問密碼;若輸入為空字串或 `key:Esc` ,則顯示 "Canceled";若輸入其他字串,則顯示 "I don't know you"

The password is checked as follows:
詢問密碼時,使用以下方式確認:

- If it equals "TheMaster", then show "Welcome!",
- Another string -- show "Wrong password",
- For an empty string or cancelled input, show "Canceled"
- 若輸入的是 "TheMaster",顯示 "Welcome!"
- 其他字串 -- 顯示 "Wrong password"
- 空字串或取消輸入,顯示 "Canceled"

The schema:
概要:

![](ifelse_task.svg)

Please use nested `if` blocks. Mind the overall readability of the code.
請使用巢狀 `if` 區塊,注意整體程式碼的可讀性。

Hint: passing an empty input to a prompt returns an empty string `''`. Pressing `key:ESC` during a prompt returns `null`.
提示:傳遞沒輸入內容的結果給 prompt 會回傳空字串 `''`。在 prompt 時按下 `key:ESC` 會回傳 `null`

[demo]
Loading