尝试使用 getElementById 在 Javascript 中打印正确答案

Trying to print the right answer in Javascript using getElementById

我想知道一个数字是否是一个完美的数字,但我无法正确打印它。数字 6、496、8128 是完美数字,但当我输入这些数字时,它一直从 var res2 而不是 var res1 打印。这里有什么问题,有人可以帮忙吗?

function perfectNo(number) {
  var temp = 0;
  var res1 = "It is a perfect number";
  var res2 = "It is not a perfect number";
  for (var i = 1; i <= number / 2; i++) {
    if (number % i == 0) {
      temp += i;
    }
  }

  if (temp == number && temp != 0) {
    document.getElementById("results").innerHTML = res1;
  } else {
    document.getElementById("results").innerHTML = res2;
  }
}
<input id="num">
<input type="button" onclick="perfectNo()" value="check">
<br>
<p>Answer:</p>
<p id="results"></p>
<br>

试试 :

var number = document.getElementById("num").value;

像这样:

function perfectNo() {
  var number = document.getElementById("num").value;
  console.log(number);
  var temp = 0;
  var res1 = "It is a perfect number";
  var res2 = "It is not a perfect number";
  for (var i = 1; i <= number / 2; i++) {
    if (number % i == 0) {
      temp += i;
    }
  }

  if (temp == number && temp != 0) {
    document.getElementById("results").innerHTML = res1;
  } else {
    document.getElementById("results").innerHTML = res2;
  }
}
<input id="num">
<input type="button" onclick="perfectNo()" value="check">
<br>
<p>Answer:</p>
<p id="results"></p>
<br>