【问题标题】:Javascript - Why won't my incorrect answer counter increment like my correct answer counter does?Javascript - 为什么我的错误答案计数器不会像我的正确答案计数器那样增加?
【发布时间】:2021-09-26 15:33:07
【问题描述】:

我正在尝试增加一个不正确的答案计数器,由于某种原因这不起作用。正确答案计数器工作正常,所以我尝试了一个类似的过程来使它成为一个函数,但没有运气。我尝试将其添加为单独的 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>

提前干杯。

【问题讨论】:

  • 您提供的代码 sn-p 有一些明显的错误
  • 你没有在任何地方调用incorrectScore()函数。

标签: javascript function if-statement increment


【解决方案1】:
if (selectedAnswer == currentQuestion.answer) {
  qualifyingPointsScore();
  correctScore();
  classToApply = 'correct-answer';

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

【讨论】:

  • 非常感谢 - 现在看起来很愚蠢......我发誓我尝试在 else 语句中一遍又一遍地调用该函数,不知道我做错了什么......可能是用另一个'if' 语句或一些愚蠢的东西。我是初学者,非常感谢。
【解决方案2】:

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

...

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

【讨论】:

  • 非常感谢 - 现在看起来很愚蠢......我发誓我尝试在 else 语句中一遍又一遍地调用该函数,不知道我做错了什么......可能是用另一个'if' 语句或一些愚蠢的东西。我是初学者,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
相关资源
最近更新 更多