title: Sum of two random variables in javascript
body: <p>In my case, I want to display the total of <em>heads+tails</em>, each time I press <code>toss</code>, so that toss in this case will be also changing dynamicaly.</p>
  
  <p>The problem is that how can I add heads+tails and show their total each time I clicks on toss.</p>
  
  <p><strong>Here is my code :</strong></p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;script type="text/javascript"&gt;
          &lt;!--
          var heads = 0;
          var tails = 0;
  
          function toss() {
              var headsField = document.getElementById("heads");
              var tailsField = document.getElementById("tails");
  
              if (flip()) {
                  ++heads;
                  headsField.value = heads;
              } else {
                  ++tails;
                  tailsField.value = tails;
              }
          }
          // end function toss
          function flip() {
              return Math.floor(Math.random() * 9) == 1
          }
          // end function flip
      &lt;/script&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form action=""&gt;
          &lt;table border="1"&gt;
              &lt;tr&gt;
                  &lt;td&gt;Heads&lt;/td&gt;
                  &lt;td&gt;&lt;input id="heads" type="text"&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Tails&lt;/td&gt;
                  &lt;td&gt;&lt;input id="tails" type="text"&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;input onclick="toss()" type="button" value="Toss"&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>