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

时间:2017-09-25 12:05:23

标签: javascript variables parentheses calculation

我使用数学和变量的组合进行javascript计算,并且我发现使用正确方法括号的难度。例如,我想进行以下计算[(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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

问题出在这一行: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>