Skip to content

Commit e0899bb

Browse files
committed
Translation zh-TW answer 71
1 parent f8a3215 commit e0899bb

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

zh-TW/README_zh-TW.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,3 +2207,42 @@ console.log('🥑' + '💻');
22072207

22082208
</p>
22092209
</details>
2210+
2211+
---
2212+
2213+
###### 71. /* 1 */ 與 /* 2 */ 該填入什麼才能輸出 console.log 之後的值?
2214+
2215+
```javascript
2216+
function* startGame() {
2217+
const answer = yield '你喜歡 JavaScript 嗎?';
2218+
if (answer !== 'Yes') {
2219+
return "哦,我想我們該走了";
2220+
}
2221+
return 'JavaScript 也愛你 ❤️';
2222+
}
2223+
2224+
const game = startGame();
2225+
console.log(/* 1 */); // 你喜歡 JavaScript 嗎?
2226+
console.log(/* 2 */); // JavaScript 也愛你 ❤️
2227+
```
2228+
2229+
- A: `game.next("Yes").value` and `game.next().value`
2230+
- B: `game.next.value("Yes")` and `game.next.value()`
2231+
- C: `game.next().value` and `game.next("Yes").value`
2232+
- D: `game.next.value()` and `game.next.value("Yes")`
2233+
2234+
<details><summary><b>答案</b></summary>
2235+
<p>
2236+
2237+
#### 答案: C
2238+
2239+
`generator` 函數在遇到 yield 關鍵字時會 “暫停” 執行。首先,我們需要讓函數產生字串 "你喜歡 JavaScript 嗎?",這可以透過呼叫 `game.next().value` 來完成。
2240+
2241+
`startGame()` 函數會一行一行執行直到遇到 `yield` 關鍵字,在函數裡第一個就有一個 `yield` 關鍵字:所以執行到第一行就停止了! _此時answer變數還尚未定義_
2242+
2243+
當我們呼叫 `game.next("Yes").value`,前一個 `yield` 被傳遞給 `next()` 的參數值所取代。此例我們使用 `Yes`。變數 `answer` 的值現在等於 `Yes`。 if 語句的條件返回 `false`,並且會返回 `JavaScript 也愛你 ❤️`
2244+
2245+
</p>
2246+
</details>
2247+
2248+
---

0 commit comments

Comments
 (0)