代码之家  ›  专栏  ›  技术社区  ›  thanos_zach

在Javascript中使用围绕变量的偏执来更改数学计算的优先级

  •  0
  • thanos_zach  · 技术社区  · 7 年前

    [(4,95/ans2)-4,5]*100 其中ans2是计算变量。在最后一个领域,我得到45.000,我应该采取-4.046。。。如果第一个和第二个字段中的输入为2+2

    <form name="Calcultor" Method="Get" id='form1'>First Number:
      <input type="text" name="first" size="35" id="first">+ Second Number:
      <input type="text" name="second" size="35" id="second">
    	
      <br>Answer:
      <input type="text" name="ans" size="35" id="ans" />
      <input type="text" name="ans2" size="35" id="ans2" />
      <input type="text" name="ans3" size="35" id="ans3" />
      <button type="button" onclick="Calculate();">Calculate</button>
    </form>
    
    <script>
      function Calculate() {
        var first = document.getElementById('first').value;
        var second = document.getElementById('second').value;
        var ans = document.getElementById('ans').value;
        var ans2 = document.getElementById('ans2').value;
    	
        document.getElementById('ans').value = parseInt(first) + parseInt(second);
        document.getElementById('ans2').value = 1.112 - 0.00043499 * parseInt(document.getElementById('ans').value) + 0.00000055 * Math.pow(parseInt(document.getElementById('ans').value), 2) - 0.00028826;
        /* in the following line i can't figure how to use with a proper way parentheses to prioriterize the calculations with the way i mentioned in the example before the code snippet*/
        document.getElementById('ans3').value = [( 4.95 / parseInt(document.getElementById('ans2').value)) - 4.5] * 100;
      }
    </script>
    1 回复  |  直到 7 年前
        1
  •  2
  •   P.S.    7 年前

    问题就在这一行: document.getElementById('ans3').value = [( 4.95 / parseInt(document.getElementById('ans2').value)) - 4.5] * 100; . 你需要使用 () 而不是 [] parseInt 价值。以下是工作片段:

    function Calculate() {
      var first = document.getElementById('first').value;
      var second = document.getElementById('second').value;
      var ans = document.getElementById('ans').value;
      var ans2 = document.getElementById('ans2').value;
    
      document.getElementById('ans').value = parseInt(first) + parseInt(second);
      document.getElementById('ans2').value = 1.112 - 0.00043499 * parseInt(document.getElementById('ans').value) + 0.00000055 * Math.pow(parseInt(document.getElementById('ans').value), 2) - 0.00028826;
      /* in the following line i can't figure how to use with a proper way parentheses to prioriterize the calculations with the way i mentioned in the example before the code snippet*/
      document.getElementById('ans3').value = ((4.95 / document.getElementById('ans2').value) - 4.5) * 100
    }
    <form name="Calcultor" Method="Get" id='form1'>First Number:
      <input type="text" name="first" size="35" id="first">+ Second Number:
      <input type="text" name="second" size="35" id="second">
    	
      <br>Answer:
      <input type="text" name="ans" size="35" id="ans" />
      <input type="text" name="ans2" size="35" id="ans2" />
      <input type="text" name="ans3" size="35" id="ans3" />
      <button type="button" onclick="Calculate();">Calculate</button>
    </form>