JavaScript: output "NaN"- 找不到错误

JavaScript: output "NaN"- Can't find the mistake

这应该是一个使用JavaScript的二次方程求根程序。我写了代码,但显然有一个错误,因为当我点击 'Calculate' 时,结果是 "x1: NaN; x2: NaN"。有人可以告诉我错误吗?

function result() {
  var a = document.getElementById('a');
  var b = document.getElementById('b');
  var c = document.getElementById('c');

  var d = Math.sqrt(b*b-4*a*c);
  var divide = (2 * a);
  var x1 = (-b + d)/divide;
  var x2 = (-b - d)/divide;

  document.write("x1: " + x1 + "<br/>");
  document.write("x2: " + x2 + "<br/>");
}

您正在为输入元素分配

var a = document.getElementById('a');

但是你需要输入的值。

var a = document.getElementById('a').value;

如果你需要Number, you could use an unary plus +

var a = +document.getElementById('a').value;
//      ^