实现三元运算符创建无限循环

Implementing ternary operator creates infinite loop

这是 JS:

function Question(question, answers, correct) {
  this.question=question;
  this.answers=answers;
  this.correct=correct;
}

Question.prototype.displayQuestion=function () {
  console.log(this.question)
  this.answers.forEach((answer, i)=> {
    console.log(`${i}: ${answer}`)
  })
}

Question.prototype.checkAnswer=function(answer) {
  (answer==this.correct)? true: false
}


var q1=new Question("What day is it today?", ["Monday", "Tuesday"], 0)
var q2=new Question("How are you?", ["Good", "Really good"], 1)
var q3=new Question("Is it nice out?", ["Yes", "No", "In-between"], 3)
var questions=[q1, q2, q3];

function createGame() {
  var num=Math.floor(Math.random()*questions.length)
  questions[num].displayQuestion()
  var userInput=parseInt(prompt("Please enter answer"))
  questions[num].checkAnswer(userInput)? console.log('You won!'): createGame()
}

createGame()

这是index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Section 5: Advanced JavaScript: Objects and Functions</title>
    </head>

    <body>
        <h1>Section 5: Advanced JavaScript: Objects and Functions</h1>
        <script src="script.js"></script>
    </body>
</html

我对这个简单程序的目标是不断输出问题,直到用户答对为止。如果用户答对了问题,您将 console.log("You won!") 并且程序将停止 运行。我在三元运算符的帮助下做到了这一点。但是,为什么我的程序永远保持运行?

checkAnswer 总是 returns 未定义因为你没有 return-ing 任何东西:

Question.prototype.checkAnswer=function(answer) {
  return answer==this.correct
}