Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I keep track of correct answers using javascript?
    text
    copied!<p>I have a simple Javascript Math program. It lets you answer equations and when it checks your answer it alerts whether you answered correctly or wrongly.</p> <p>Here is the code for addition:</p> <pre><code>function randomAdd() { var x=document.getElementById("AddN1"); x.value=Math.floor((Math.random()*12)+1); var y=document.getElementById("AddN2"); y.value=Math.floor((Math.random()*12)+1); } function checkAdd() { var z=document.getElementById("AddA2"); z.value= parseInt(document.getElementById("AddN1").value) + parseInt(document.getElementById("AddN2").value); if(parseInt(document.getElementById("AddA1").value)==z.value) { score=score+1; alert("Addition: Correct"); } else { alert("Addition: Incorrect"); } } &lt;form name="Addition"&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;Addition&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;Number 1&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;Number 2&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;Type Your Answer&lt;/th&gt; &lt;th&gt;Correct Answer&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" name="AddN1" id="AddN1"&gt;&lt;/td&gt; &lt;td&gt;+&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="AddN2" id="AddN2"&gt;&lt;/td&gt; &lt;td&gt;=&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="AddA1" id="AddA1"&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="AddA2" id="AddA2"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="button" value="Get Numbers" onclick="randomAdd();"&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="button" value="Check Answer" onclick="checkAdd();"&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>The code was provided to me by my college. I would like to keep a simple counter that will count all the correct answers the user gets and also a reset button that would reset the counter back to zero?</p> <p>Sorry if this sounds really basic but I have no experience with Javascript.</p> <p>Thanks.</p>
 

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