在这里的代码中,我试图让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>
&#13;
答案 0 :(得分:3)
问题是secondGLow
和secondGHigh
是字符串。如果要执行数学计算,则必须将这些转换为数字,否则+
将执行字符串连接。
试试这个:
var secondGLow = parseInt(document.getElementById("secondGLow").value),
secondGHigh = parseInt(document.getElementById('secondGHigh').value);