简体   繁体   English

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

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

I'm trying to increment an incorrect answer counter, for some reason this isn't working.我正在尝试增加一个不正确的答案计数器,由于某种原因,这不起作用。 The correct answer counter is working fine so I've tried a similar process in making it a function but no luck.正确答案计数器工作正常,因此我尝试了类似的过程使其成为功能,但没有运气。 I've tried adding it as separate if statement and also inside the if (selectedAnswer == currentQuestion.answer) statement.我尝试将它添加为单独的 if 语句,并在if (selectedAnswer == currentQuestion.answer)语句中添加。

You can ignore the qualifying points because that is part of another counter, I just also want a counter that increments correct and incorrect answers.您可以忽略合格点数,因为它是另一个计数器的一部分,我还想要一个计数器来增加正确和不正确的答案。 The correctScore () function works fine, so why doesn't the incorrect one when I place it after correctScore (); correctScore ()函数工作正常,那么当我把它correctScore ();之后时为什么不正确的呢correctScore (); ? ? Hope that makes sense.希望这是有道理的。

 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>

Cheers in advance.提前加油。

You need to add an else part to the if-statement您需要在 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的购物车增量计数器不起作用? - Why won't my cart increment counter work? 为什么这个不正确的 JavaScript 程序会产生正确的答案? - Why does this incorrect JavaScript program produce the correct answer? 为什么我的for循环返回正确的答案,但我的forEach没有? - Why does my for loop return the correct answer, but my forEach does not? 为什么我的 function 对于平均工资问题返回错误答案 - Why does my function return an incorrect answer for the average salary question JavaScript错误查找有关为什么我的商品数量*商品成本无法给出最终答案的错误 - JavaScript Error Finding the error on why my qty of items *cost of items won't give me a final answer 为什么我的Javascript无法为Euler项目的第一号返回正确答案? - Why isn't my javascript returning the correct answer to Project Euler's Number One? 为什么单击画布后计数器无法工作? - Why won't my counter work upon clicking the canvas? 为什么我的柜台无法像我期望的那样在现场工作 - Why does my counter not work in the field like I would expect 在特定条件下增加计数器(例如:仅当用户未输入错误答案时) - increment a counter under certain condition (ex: only if user didn't input wrong answer) 为什么我的 JavaScript 第二个计数器不工作? - Why is my JavaScript second counter not working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM