Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 +1,2 @@
No difference.
沒有不同。

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 4

---

# Is "else" required?
# "else" 是否必須?

The following function returns `true` if the parameter `age` is greater than `18`.
底下的函式中,若參數 `age` 大於 `18` 時會回傳 `true`。

Otherwise it asks for a confirmation and returns its result:
否則它會要求確認並回傳結果:

```js
function checkAge(age) {
Expand All @@ -21,7 +21,7 @@ function checkAge(age) {
}
```

Will the function work differently if `else` is removed?
若把 `else` 刪掉,此函式是否會以不同方式運作?

```js
function checkAge(age) {
Expand All @@ -35,4 +35,5 @@ function checkAge(age) {
}
```

Is there any difference in the behavior of these two variants?
這兩種做法在行為上是否有任何不同?

Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
Using a question mark operator `'?'`:
使用問號運算子 `'?'`

```js
function checkAge(age) {
return (age > 18) ? true : confirm('Did parents allow you?');
}
```

Using OR `||` (the shortest variant):
使用 OR `||`(最短的做法):

```js
function checkAge(age) {
return (age > 18) || confirm('Did parents allow you?');
}
```

Note that the parentheses around `age > 18` are not required here. They exist for better readabilty.
注意圍繞著 `age > 18` 的括號在此並非必要,它們存在的原因只是為了更佳的可讀性。

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 4

---

# Rewrite the function using '?' or '||'
# 使用 '?' '||' 改寫函式

The following function returns `true` if the parameter `age` is greater than `18`.
底下的函式中,若參數 `age` 大於 `18` 則回傳 `true`。

Otherwise it asks for a confirmation and returns its result.
否則它要求確認並回傳其結果。

```js
function checkAge(age) {
Expand All @@ -18,9 +18,10 @@ function checkAge(age) {
}
```

Rewrite it, to perform the same, but without `if`, in a single line.
改寫它,使其在沒有 `if` 的情況下,以單獨一行就可以達到同樣的操作。

Make two variants of `checkAge`:
寫出兩種 `checkAge` 的不同做法:

1. 使用問號運算子 `?`
2. 使用 OR `||`

1. Using a question mark operator `?`
2. Using OR `||`
7 changes: 4 additions & 3 deletions 1-js/02-first-steps/14-function-basics/3-min/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
A solution using `if`:
使用 `if` 的解法:

```js
function min(a, b) {
Expand All @@ -10,12 +10,13 @@ function min(a, b) {
}
```

A solution with a question mark operator `'?'`:
使用問號運算子 `'?'` 的解法:

```js
function min(a, b) {
return a < b ? a : b;
}
```

P.S. In the case of an equality `a == b` it does not matter what to return.
註:在相等的情況 `a == b`,回傳哪一個就不重要了。

6 changes: 3 additions & 3 deletions 1-js/02-first-steps/14-function-basics/3-min/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 1

---

# Function min(a, b)
# 函式 min(a, b)

Write a function `min(a,b)` which returns the least of two numbers `a` and `b`.
寫出一個函式 `min(a, b)`,用以回傳 `a` `b` 兩個數值中的最小者。

For instance:
舉個例:

```js
min(2, 5) == 2
Expand Down
1 change: 1 addition & 0 deletions 1-js/02-first-steps/14-function-basics/4-pow/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ if (n < 1) {
alert( pow(x, n) );
}
```

9 changes: 5 additions & 4 deletions 1-js/02-first-steps/14-function-basics/4-pow/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ importance: 4

---

# Function pow(x,n)
# 函式 pow(x, n)

Write a function `pow(x,n)` that returns `x` in power `n`. Or, in other words, multiplies `x` by itself `n` times and returns the result.
寫一個函式 `pow(x, n)`,用以回傳 `x` `n` 次方。換句話說,將 `x` 自乘 `n` 次並回傳其結果。

```js
pow(3, 2) = 3 * 3 = 9
pow(3, 3) = 3 * 3 * 3 = 27
pow(1, 100) = 1 * 1 * ...* 1 = 1
```

Create a web-page that prompts for `x` and `n`, and then shows the result of `pow(x,n)`.
建立一個網頁,分別跳出提示輸入 `x` `n` 的 prompts,並顯示 `pow(x, n)` 的結果。

[demo]

P.S. In this task the function should support only natural values of `n`: integers up from `1`.
註:在本課題中函式應該要只支援自然數 `n`:從 `1` 開始向上數的整數。

Loading