OutputNot attempted
Asking questions with a generator
function* startGame() { const answer = yield 'Do you love JavaScript?'; if (answer !== 'Yes') return "Oh wow... Guess we're done here"; return 'JavaScript loves you back';}const game = startGame();console.log(game.next().value);console.log(game.next('Yes').value);console.log(game.next().value);Spaces and quote style don’t matter · write a thrown error as TypeError
Ctrl Enter
Show answer & explanation
The expected output comes from actually running the snippet; opening this shows it next to the explanation.
The first next() runs to the yield and hands out the question. next("Yes") resumes with answer = "Yes" and the function returns. A finished generator yields undefined forever.