My quiz has a bug in where if the correct answer is pressed multiple times, the score increases an infinite number of times?

huangapple go评论58阅读模式
英文:

My quiz has a bug in where if the correct answer is pressed multiple times, the score increases an infinite number of times?

问题

It seems like you're facing an issue with your CSS animation quiz where the score increases even if the correct answer has already been selected. To prevent this, you can disable the answer boxes after a correct answer is selected, but it seems you encountered issues with your previous attempt.

You can try this approach to disable the answer boxes and still allow the next button to work:

// Add this variable to keep track of whether an answer has been selected
var answerSelected = false;

$('[class^="answer"]').click(function(){
  // Check if an answer has already been selected
  if (!answerSelected) {
    $(this).addClass("selected");
    setTimeout(function() {
      checkAnswer(1);
    }, 1000);

    // Set the answerSelected variable to true to prevent further selections
    answerSelected = true;
  }
});

// Reset the answerSelected variable when moving to the next question
$('#next-btn').click(function(){
  answerSelected = false;
  nextQuestion();
});

This code adds a variable answerSelected to track whether an answer has been selected. It disables further answer selections once an answer has been chosen but resets it when moving to the next question. This should prevent the score from increasing when the correct answer is pressed more than once.

英文:

so, I am creating a css animation for my degree, and I have the quiz setup so that it cycles through questions with specific answers, the quiz is working fully functional etc...however, I have noticed that when the correct answer is pressed more than once, the score increases, even if the answer has already been selected and initial score increase has been added. it begins with a screen that has text on it offering a narative to the game we are creating, and this works as intended also.

It is a single page set-up where all pages are inside of one parent container and to cylce pages it calls on a child container.

code that is being used it all JQuery and HTML with CSS to style it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    // --------------------------Multiple Choice Page------------------------//

    $(&quot;#start-button&quot;).click(function() {
      $(&quot;#beginning-screen&quot;).remove();
      ;})

      $(&quot;#start-button-quiz&quot;).click(function() {
        $(&quot;#beginning-screen-quiz&quot;).remove();
        ;})

    $(&#39;#answer1&#39;).click(function(){
      $(this).addClass(&quot;selected&quot;);
      setTimeout(function() {
          checkAnswer(1);
      },1000);
    });
    $(&#39;#answer2&#39;).click(function(){
      $(this).addClass(&quot;selected&quot;)
      setTimeout(function() {
          checkAnswer(2);
      },1000);
    });
    $(&#39;#answer3&#39;).click(function(){
      $(this).addClass(&quot;selected&quot;)
      setTimeout(function() {
          checkAnswer(3);
      },1000);
    });

    $(&#39;#next-btn&#39;).click(function(){
      nextQuestion();
    });
    nextQuestion();

});

// --------------------------- Multiple Choice Page functions and variables -------------------------------------

// Study Page

var currentBox = 1;
var numBoxes = $(&#39;.text-box&#39;).length;


// variables here

var quizData = [
  {question:&quot;If the size of a battery in a complete circuit is increased:&quot;,
   answers:[&quot;the current decreases&quot;,&quot;the current increases&quot;,&quot;the voltage stays the same&quot;],
   correctAnswer:2,
   feedback:&quot;An increase in voltage will increase the current flowing in a circuit (if nothing else is changed) Click to next page find out more.&quot;
  },
   {question:&quot;The current in a series circuit containing two bulbs:&quot;,
   answers:[&quot;is the same at every place in the circuit&quot;,&quot;is larger nearer the battery&quot;,&quot;is larger between the bulbs&quot;],
   correctAnswer:1,
   feedback:&quot;This is an important point to remember about a series circuit. Click to next page find out more.&quot;
  },
  {question:&quot;Two identical bulbs are in parallel in a complete circuit. If one breaks:&quot;,
   answers:[&quot;the other stays on&quot;,&quot;the other goes off&quot;,&quot;the other bulb is brighter&quot;],
   correctAnswer:1,
   feedback:&quot;This is a benefit of a parallel circuit, it is usually easier to find out which component has broken as the rest still work&quot;
  },
  {question:&quot;Wires are usually coated in plastic because:&quot;,
   answers:[&quot;plastic is an insulator&quot;,&quot;plastic can be lots of different colours&quot;,&quot;plastic helps the wires conduct electricity&quot;],
   correctAnswer:1,
   feedback:&quot;Wires used to be covered with rubber as an insulator but rubber perishes faster than plastic.&quot;
  },
  {question:&quot;Which of the following statements is true?&quot;,
   answers:[&quot;A voltmeter is connected in series to measure voltage.&quot;,&quot;Different sized bulbs in parallel have the same current flowing through them.&quot;,&quot;In a parallel circuit with four identical bulbs, the current is shared equally between them.&quot;],
   correctAnswer:3,
   feedback:&quot;If the four bulbs were NOT identical, the current would still be shared out, but not equally.&quot;
  },
  {question:&quot;Wires are normally made from copper. This is because:&quot;,
   answers:[&quot;copper has a high resistance&quot;,&quot;copper is cheap&quot;,&quot;copper is a very good conductor of electricity&quot;],
   correctAnswer:3,
   feedback: &quot;It is also very malleable and can therefore easily be bent to go round corners.&quot;
  },
  {question:&quot;Two identical bulbs are in parallel in a complete circuit. A third identical bulb is connected in parallel. What happens?&quot;,
   answers:[&quot;All the bulbs are dimmer&quot;,&quot;All the bulbs are the same brightness&quot;,&quot;The third bulb is brighter&quot;],
   correctAnswer:2,
   feedback:&quot;Adding identical bulbs in parallel uses more current but brightness stays the same.&quot;
  },
];


var currentQuestion = 0;
var totalQuestions = quizData.length;
var score = 0;

function showQuestion(){
$(&quot;#question&quot;).html(quizData[currentQuestion-1].question);
$(&quot;#answer1&quot;).html(quizData[currentQuestion-1].answers[0]);
$(&quot;#answer2&quot;).html(quizData[currentQuestion-1].answers[1]);
$(&quot;#answer3&quot;).html(quizData[currentQuestion-1].answers[2]);
$(&quot;#feedback&quot;).html(quizData[currentQuestion-1].feedback);
}

function nextQuestion(){
currentQuestion++;

if(score &gt;= 60){
  $(&quot;#answersAndFeedback&quot;).addClass(&#39;hidden&#39;);
  $(&quot;#question&quot;).addClass(&#39;hidden&#39;);
  $(&quot;#game-over&quot;).removeClass(&#39;hidden&#39;);
  $(&#39;.p2-button&#39;).removeClass(&#39;hidden&#39;);
  $(&quot;#next-btn&quot;).addClass(&quot;hidden&quot;);
  return;
}

if(currentQuestion &gt; totalQuestions){
  // end of quiz
  $(&quot;#game-over&quot;).removeClass(&#39;hidden&#39;);
  return;
}

showQuestion();

// hide feedback and next button
$(&quot;#next-btn&quot;).addClass(&quot;hidden&quot;);
$(&quot;#feedback&quot;).addClass(&quot;hidden&quot;);
// remove all incorrect, correct classes on answer buttons
$(&#39;.answer-box&#39;).removeClass(&quot;correct incorrect&quot;);

// add restart button if score is under 60
if(score &lt; 60){
  $(&quot;#feedback&quot;).append(&#39;&lt;button id=&quot;restart-btn&quot; onclick=&quot;location.reload();&quot;&gt;Restart Quiz&lt;/button&gt;&#39;);
  $(&#39;.p2-button&#39;).addClass(&#39;hidden&#39;);
}
}


function checkAnswer(selectedAnswer){
var theCorrectAnswer = quizData[currentQuestion-1].correctAnswer;
// remove the grey selected class
$(&#39;.answer-box&#39;).removeClass(&quot;selected&quot;);

// turn the boxes red or green depending on if they are the correct answer
$( &quot;.answer-box&quot; ).each(function( index ) {
  if((index+1)==theCorrectAnswer){
    $( this ).addClass(&quot;correct&quot;);
  } else {
    $( this ).addClass(&quot;incorrect&quot;);
  }
});

if(selectedAnswer==theCorrectAnswer){
  // got it correct
  score += 10;
  $(&quot;.score&quot;).html(score);
} else {
  // got it wrong so do nothing
}
// show feedback and next button
$(&quot;#next-btn&quot;).removeClass(&quot;hidden&quot;);
$(&quot;#feedback&quot;).removeClass(&quot;hidden&quot;);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;!-- Multiple Choice Page --&gt;

&lt;div class=&quot;page-multiple-choice&quot;&gt;
    &lt;div id=&quot;beginning-screen-quiz&quot;&gt;
        &lt;h1 id=&quot;heading-quiz&quot;&gt;Oh no! it looks like you have been locked out of the computer system. You will have to take this quiz in order to regain access, you must get a score of 60 in order to bypass the security system.&lt;/h1&gt;
        &lt;button id=&quot;start-button-quiz&quot;&gt;Hack into security&lt;/button&gt;
    &lt;/div&gt;
    &lt;h1&gt;Multiple Choice Quiz&lt;/h1&gt;
    &lt;div class=&quot;p2-button button&quot;&gt;
        Fix the circuit
    &lt;/div&gt;
    &lt;div class=&quot;p-menu-button button&quot;&gt;
        Go back to menu
    &lt;/div&gt;
    &lt;div id=&quot;wrapper&quot;&gt;
        &lt;div id=&quot;question&quot;&gt;Sample question here&lt;/div&gt;
        &lt;div id=&quot;answersAndFeedback&quot;&gt;
            &lt;div id=&quot;answer1&quot; class=&quot;answer-box&quot;&gt;Answer 1&lt;/div&gt;
            &lt;div id=&quot;answer2&quot; class=&quot;answer-box&quot;&gt;Answer 2&lt;/div&gt;
            &lt;div id=&quot;answer3&quot; class=&quot;answer-box&quot;&gt;Answer 3&lt;/div&gt;
            &lt;div id=&quot;feedback&quot; class=&quot;hidden&quot;&gt;Feedback goes here&lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;score&quot;&gt;0&lt;/div&gt;
        &lt;div id=&quot;next-btn&quot; class=&quot;hidden&quot;&gt;next&lt;/div&gt;
        &lt;div id=&quot;game-over&quot; class=&quot;hidden&quot;&gt;Congratulations! Continue to the next page!&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

any explanation how to fix this would be greatly appreciated, although the bug may not be found by users, I would still like to cover all basis and make sure it is fixed for the integrity of the game design.

I tried by adding:

// Disable all answer boxes to prevent further selection
  $(&#39;.answer-box&#39;).prop(&#39;disabled&#39;, true);

however, an error came up in developer mode on google, so I removed that, but it would still not stop the issue I was having and then that also created more issues by not allowing me to press the next button and would completely disable the selection process after the first correct answer was pressed.

答案1

得分: 1

这里我在分数增加时添加了calculated类。

尝试这个 JSFiddle

function checkAnswer(selectedAnswer) {
    var theCorrectAnswer = quizData[currentQuestion - 1].correctAnswer;
    // 移除灰色选定类
    $('.answer-box').removeClass("selected");

    // 根据是否是正确答案将框变红或绿
    $(".answer-box").each(function (index) {
        if ((index + 1) == theCorrectAnswer) {
            $(this).addClass("correct");
        } else {
            $(this).addClass("incorrect");
        }
    });

    if (selectedAnswer == theCorrectAnswer && $(".score").hasClass('calculated') == false) {
        // 回答正确
        score += 10;
        $(".score").html(score);
        $(".score").addClass('calculated');
    } else {
        // 回答错误,不执行任何操作
    }
    // 显示反馈和下一步按钮
    $("#next-btn").removeClass("hidden");
    $("#feedback").removeClass("hidden");
}

并在nextQuestion()的开头添加:

$(".score").removeClass('calculated');
英文:

Here I have added calculated class when score increased

Try this JSFiddle

function checkAnswer(selectedAnswer) {
    var theCorrectAnswer = quizData[currentQuestion - 1].correctAnswer;
    // remove the grey selected class
    $(&#39;.answer-box&#39;).removeClass(&quot;selected&quot;);

    // turn the boxes red or green depending on if they are the correct answer
    $(&quot;.answer-box&quot;).each(function (index) {
        if ((index + 1) == theCorrectAnswer) {
            $(this).addClass(&quot;correct&quot;);
        } else {
            $(this).addClass(&quot;incorrect&quot;);
        }
    });

    if (selectedAnswer == theCorrectAnswer &amp;&amp; $(&quot;.score&quot;).hasClass(&#39;calculated&#39;) == false) {
        // got it correct
        score += 10;
        $(&quot;.score&quot;).html(score);
        $(&quot;.score&quot;).addClass(&#39;calculated&#39;);
    } else {
        // got it wrong so do nothing
    }
    // show feedback and next button
    $(&quot;#next-btn&quot;).removeClass(&quot;hidden&quot;);
    $(&quot;#feedback&quot;).removeClass(&quot;hidden&quot;);
}

And add in start of nextQuestion()

$(&quot;.score&quot;).removeClass(&#39;calculated&#39;);

答案2

得分: 0

我认为你需要遍历你的答案框。

根据这个问题和以下答案的示例(https://stackoverflow.com/questions/4735342/jquery-to-loop-through-elements-with-the-same-class),你应该将你的JS代码更改为:

$('.answer-box').each(function(){
     $(this).prop('disabled', true);
 });
英文:

I think you have to iterate through your answer-boxes.

Adapting the example of this question and the following answers (https://stackoverflow.com/questions/4735342/jquery-to-loop-through-elements-with-the-same-class), you should change your JS-code to:

 $(&#39;.answer-box&#39;).each(function(){
     $(this).prop(&#39;disabled&#39;, true);
 });

huangapple
  • 本文由 发表于 2023年5月11日 20:16:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定