在Javascript中使用变量周围的括号来更改数学计算的优先级 [英] Using parantheses around variables in Javascript to change priority in math calculations

查看:41
本文介绍了在Javascript中使用变量周围的括号来更改数学计算的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数学和变量的组合来进行javascript计算,并且在使用正确方式的括号时遇到了困难.例如,我想执行以下计算 [(4,95/ans2)-4,5] * 100 ,其中ans2是一个计算变量.在最后一个字段中,我得到45.000,我应该取-4.046 ...如果第一个字段和第二个字段中的输入为2 + 2

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>

推荐答案

问题出在以下行: document.getElementById('ans3').value = [(4.95/parseInt(document.getElementById('ans2').value))-4.5] * 100; .您需要使用()而不是 [] 进行分组,并且也不需要 parseInt 值.这是有效的代码段:

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>

这篇关于在Javascript中使用变量周围的括号来更改数学计算的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆