title: How to overcome undefined error in javascript
body: <p>Here is my code. You can also find it at <a href="http://jsfiddle.net/nE72g/" rel="nofollow">a jsfiddle</a>.</p>
  
  <pre><code>&lt;form name="myform" method="post" onsubmit="return arith()"&gt;
        &lt;center&gt;&lt;table&gt;
                &lt;tr&gt;&lt;td&gt;&lt;font face="verdana,arial" size=-1&gt;Value 1 &lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;input type = "text" id = "num1"&gt;&lt;/td&gt;&lt;/tr&gt;
                &lt;tr&gt;&lt;td&gt;&lt;font face="verdana,arial" size=-1&gt;Value 2 &lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;input type = "text" id = "num2"&gt;&lt;/td&gt;&lt;/tr&gt;
                &lt;tr&gt;&lt;td&gt;&lt;font face="verdana,arial" size=-1&gt;&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="checkbox" name="check" value="Addition" id="check1"/&gt;Addition&lt;/td&gt;&lt;/tr&gt;
                &lt;tr&gt;&lt;td&gt;&lt;font face="verdana,arial" size=-1&gt;&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="checkbox" name="check" value="Subtraction" id="check2"/&gt;Subtraction&lt;/td&gt;&lt;/tr&gt;
                &lt;tr&gt;&lt;td&gt;&lt;font face="verdana,arial" size=-1&gt;&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="checkbox" name="check" value="Multiplication" id="check3"/&gt;Multiplication&lt;/td&gt;&lt;/tr&gt;
                &lt;tr&gt;&lt;td&gt;&lt;font face="verdana,arial" size=-1&gt;&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="checkbox" name="check" value="Division" id="check4"/&gt;Division&lt;/td&gt;&lt;/tr&gt;
                &lt;tr&gt;&lt;td&gt;&lt;font face="verdana,arial" size=-1&gt;&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;font face="verdana,arial" size=-1&gt;&lt;input type = "submit" value = "Submit"&gt;&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
          &lt;/table&gt;&lt;/center&gt;
   &lt;/form&gt;
   &lt;p id="demo"&gt;&lt;/p&gt;   
  </code></pre>
  
  <p>Javascript code:</p>
  
  <pre><code>&lt;script&gt;
          var op;
          function changeCheckBox(val) {
              try {
                  var i;
                  var max = document.myform.check.length;
                  var count = 0;
                  op = val;
  
                  for (i = 0; i &lt; max; i++) {
                      if (document.myform.check[i].checked === true) {
                          count++;
                          serNoChecked = i;
                      }
                  }
                  if (count === 1) {
                      for (i = 0; i &lt; max; i++) {
                          if (document.myform.check[i].checked === false) {
                              document.myform.check[i].disabled = true;
                          }
                      }
                  } else if (count === 0) {
                      for (i = 0; i &lt; max; i++) {
                          document.myform.check[i].disabled = false;
                      }
                  }
  
                  if (null === max) return false;
                  if (count === 0) {
                      return true;
                  } else if (count &gt; 0) {
                      return false;
                  }
              } catch (e) {
                  alert(e.message);
              }
          }
  
          function arith() {
              var n1 = parseInt(document.getElementById('num1').value, 10);
              var n2 = parseInt(document.getElementById('num2').value, 10);
               var newVal1;
               var newVal2;
               var newVal3;
               var newVal4;
  
              if (op == "Addition") {
                  newVal1 = n1 + n2;
              } else if (op == "Subtraction") {
                  newVal2 = n1 - n2;
              } else if (op == "Multiplication") {
                  newVal3 = n1 * n2;
              } else if (op == "Division") {
                  newVal4 = n1 / n2;
              }
  
              var demoP = document.getElementById("demo");
              {
                  var html = "";
                  html += "Addition =" + newVal1 + "&lt;br/&gt;";
                  html += "Subtraction =" + newVal2 + "&lt;br/&gt;";
                  html += "Multiplication =" + newVal3 + "&lt;br/&gt;";
                  html += "Division =" + newVal4 + "&lt;br/&gt;";
                  demoP.innerHTML = html;
              }
              return false;
          }
   &lt;/script&gt;
  </code></pre>
  
  <p>While running it, I am getting undefined error for all the four operations.
  If the user enters the two values and if they click all the checkboxes, it should display all the outputs corresponding to the arithmetic operations. How to do that?</p>