-->

Using parantheses around variables in Javascript t

2019-09-14 21:52发布

问题:

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>