Note that there are some explanatory texts on larger screens.

plurals
  1. POvariable doesn't update when function restarts (javascript)
    primarykey
    data
    text
    <p>I'm going to try my best to ask this question without a huge wall of code. Basically I have written a very simple math quiz game. In this game you select a difficulty, the number of questions you want, and the game starts. It asks that number of questions and then you get a score and then the game is over. However, you can restart the game. When you restart, it simply returns you to the home screen, then you can select your options again. The only problem is, we need to keep track of the number of questions remaining in the quiz, the first time around, it works well. We pass <code>numQuestions</code> to the game function. The second time, however, even if I pass <code>numQuestions=10</code>, the value remains 0 from the first time I played the game. Here is the game function:</p> <pre><code>function startGame(difficulty,numQuestions,score){ // begins game, excluded that come its just some acsii art // asks question var q = new question(difficulty); $("#gameInside").html(q.string); $("#gameInside").data('answer',q.answer); // gives answer options for (var ii=0;ii&lt;=3;ii++){ $("#answers").append("&lt;button class='answerButton'&gt;"+q.answerArray[ii]+"&lt;/button&gt;") } // starts timer var b = document.getElementById("timer"); timer = new stopWatch(b, {delay: 100}); timer.start(); },5500) // when answer is clicked, go here $("#gameScreen").on("click",".answerButton",function() { // this seems to be the problem: on the second time I play the game, numQuestions remains the value from the first game and continues to go down (-1,-2) and since my selector is (&gt;0), the else statement fires. numQuestions--; var time = parseFloat($("#timer span").html()); var correct = parseFloat($("#gameInside").data('answer')); var userAnswer = parseFloat($(this).html()); if (correct==userAnswer) tempScore = Math.round(calculateScore(time)*100)/100; else tempScore = 0; score += tempScore; $("#score").html(Math.round(score*100)/100); if (numQuestions &gt; 0) { var q = new question(difficulty); $("#gameInside").html(q.string); $("#gameInside").data('answer',q.answer); $("#answers").empty(); for (var ii=0;ii&lt;=3;ii++){ $("#answers").append("&lt;button class='answerButton'&gt;"+q.answerArray[ii]+"&lt;/button&gt;") } timer.reset(); } else { $("#answers").empty(); $("#gameInside").html('Game Over! Thanks for Playing!'); timer.stop(); } }); } </code></pre> <p>any ideas? Thanks</p> <p>edit:</p> <pre><code>$(".numberQButton").click(function(){ numQuestions = parseFloat($(this).html()); $("#numQuestionsScreen").hide(); $("#gameScreen").show(); $("#score").html(0); startGame(difficulty,numQuestions,score); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload