在这里的代码中,我试图让 secondGLow 被使用两次而不完全改变它。
我的例子是
(a - b) / 10 * 2 + b = (secondGHigh - secondGLow)/10 * 2 + secondGLow
这是错误的。正确答案应该是 12
20 - 10 / 10 * 2 + 10 = 210
function calc() {
var secondGLow = document.getElementById("secondGLow").value,
secondGHigh = document.getElementById('secondGHigh').value;
var secondEquation = ((secondGHigh - secondGLow) / 10 * 2 + secondGLow);
document.getElementById('2ndGear').innerHTML = "2nd Gear: " + secondEquation;
}
<input type="number" id="secondGLow" placeholder="Enter low gear value here">
<input type="number" id="secondGHigh" placeholder="Enter high gear value here">
<button onclick="calc()">calc</button>
<br />
<br />
<label id="2ndGear"></label>
最佳答案
问题是 secondGLow
和 secondGHigh
是字符串。如果您想执行数学计算,则必须将它们转换为数字,否则 +
将执行字符串连接。
试试这个:
var secondGLow = parseInt(document.getElementById("secondGLow").value),
secondGHigh = parseInt(document.getElementById('secondGHigh').value);
关于javascript - 如何多次使用多个变量而不混淆它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48539071/