Using parantheses around variables in Javascript to change priority in math calculations

可紊 提交于 2019-12-14 03:05:18

问题


I'm exercising in javascript calculations using a combination of maths and variables and i'm finding a difficulty on using the right way parentheses. For example i want to do the following calculation [(4,95/ans2)-4,5]*100 where ans2 is a calculated variable. In the last field i get 45.000 and i should take - 4.046 ... if the input in the first and second fields was 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:


The issue was in this row: document.getElementById('ans3').value = [( 4.95 / parseInt(document.getElementById('ans2').value)) - 4.5] * 100;. You need to use () instead of [] for grouping and you also don't need to parseInt the value. Here is the working snippet:

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>


来源:https://stackoverflow.com/questions/46404818/using-parantheses-around-variables-in-javascript-to-change-priority-in-math-calc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!