二次计算器给出了Nan

时间:2018-04-09 21:38:33

标签: javascript math

我正在写一个反应应用程序来解决二次公式。当我输入a=1 b=7 c=10或二次方的任何有效数字时,它会返回NaN[Object]NaN。有人可以帮忙,这是我的代码:

  quadraticFormula(a, b, c) {
    var root = -b + Math.sqrt(b, 2) - (4 * a * c);
    var plusAnswer = (-b + Math.sqrt(root))/(2*a);
    var minusAnswer = (-b + Math.sqrt(root))/(2*a);

    document.getElementById('answers').innerHTML = plusAnswer + <br/> + minusAnswer;
  }

1 个答案:

答案 0 :(得分:1)

root的公式不正确。它应该是Math.sqrt((b * b) - (4 * a * c))

此外,您未在-中使用minusAnswer

function quadraticFormula(a, b, c) {
  var root = Math.sqrt((b * b) - (4 * a * c))
  var plusAnswer = (-b + root) / (2 * a)
  var minusAnswer = (-b - root) / (2 * a)

  document.getElementById('answers').innerHTML = plusAnswer + <br/> + minusAnswer;
}

HTH