JavaScript簡易計算機の効果


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>       </title>
</head>
<body>
  <form name="myForm">
      1:<input type="text" name="num1" />
    <br>
      2:<input type="text" name="num2" />
    <br>
       :<input type="text" name="res"/>
  </form>
  <br>
  <button onclick="Calculation('+')">+</button>
  <button onclick="Calculation('-')">-</button>
  <button onclick="Calculation('*')">*</button>
  <button onclick="Calculation('/')">/</button>
  <script type="text/javascript">
      function Calculation(e){
        var num1 = document.myForm.num1.value;
        var num2 = document.myForm.num2.value;
        var res = null;
        //      ,      
        num1 = parseFloat(num1);
        num2 = parseFloat(num2);
        if(isNaN(num1)){
          alert('  1   ,      ');
          return false;
        }
        if(isNaN(num2)){
          alert('  2   ,      ');
          return false;
        }

        //     
        switch(e){
          case '+':
            res = num1 + '+' + num2 + "=" + (num1 + num2);
            break;
          case '-':
            res = num1 + '-' + num2 + "=" + (num1 - num2);
            break;
          case '*':
            res = num1 + '*' + num2 + "=" + (num1 * num2);
            break;
          case '/':
            if(num2 == 0){
              alert('  2    ');
              return false;
            }
            res = num1 + '/' + num2 + "=" + (num1 / num2);
            break;
        }

        //          
        document.myForm.res.value = res;
      }
  </script>
</body>
</html>