Javascript - 为什么我的错误答案计数器不会像我的正确答案计数器那样递增?

Javascript - Why won't my incorrect answer counter increment like my correct answer counter does?

我正在尝试增加错误答案计数器,但由于某种原因,这不起作用。正确答案计数器工作正常,所以我尝试了一个类似的过程来使它成为一个函数,但没有成功。我试过将它作为单独的 if 语句添加,也在 if (selectedAnswer == currentQuestion.answer) 语句中添加。

你可以忽略合格分数,因为它是另一个计数器的一部分,我还想要一个计数器来增加正确和错误的答案。 correctScore () 函数工作正常,为什么我把它放在 correctScore (); 之后不正确呢? 希望这是有道理的。

let classToApply = 'incorrect-answer';
if (selectedAnswer == currentQuestion.answer) {
  qualifyingPointsScore();
  correctScore();
  classToApply = 'correct-answer';

  /*selectedChoice.parentElement.classList.add(classToApply);*/
}

function correctScore() {
  correctAnswers += 1;
  document.getElementById("correct-score").innerText = correctAnswers;
}

function incorrectScore() {
  incorrectAnswers += 1;
  document.getElementById("incorrect-score").innerText = incorrectAnswers;
}
</div>
<div class="scores-container">
  <div class="scores-area">
    <p class="scores">Correct Answers: <span id="correct-score">0</span></p>
    <p class="scores">Incorrect Answers: <span id="incorrect-score">0</span></p>
    <p class="scores">Total Questions <span id="question-counter">0</span></p>
  </div>
</div>

提前干杯。

您需要在 if 语句中添加一个 else 部分

...

if (selectedAnswer == currentQuestion.answer) {
  qualifyingPointsScore();
  correctScore();
  classToApply = 'correct-answer';
 } else {
  incorrectScore();
 }
 
...
if (selectedAnswer == currentQuestion.answer) {
  qualifyingPointsScore();
  correctScore();
  classToApply = 'correct-answer';

  /*selectedChoice.parentElement.classList.add(classToApply);*/
}
else{
incorrectScore();
}