这个简单的测试怎么了?

时间:2019-07-09 19:59:11

标签: javascript

我只是使用一个简单的比较功能。但是结果并不一致。

var submitButton = document.getElementById("Submit");

document.getElementById("demo").innerHTML = "Answer";

function CheckNums(num1, num2) {
    if (num1 == num2) {
        return "EQUAL";
    } else if (num1 > num2) {
        return "YES";
    } else if (num2 > num1) {
        return "NOPE";
    }
}

submitButton.addEventListener('click', function() {
    var firstNum = document.getElementById("firstNum").value;
    var secondNum = document.getElementById("secondNum").value;
    console.log(firstNum, secondNum);
    document.getElementById("demo").innerHTML = CheckNums(firstNum, secondNum);
});

10大于0或1,但不大于5。

1 个答案:

答案 0 :(得分:0)

使用 + 将字符串转换为数字,如下所示:

var submitButton = document.getElementById("Submit");

document.getElementById("demo").innerHTML = "Answer";

function CheckNums(num1,num2){ 
  if(num1 == num2){
    return "EQUAL";
  }
  else if(num1 > num2){
    return "YES"
  ;
  }  
  else if(num2 > num1){
    return "NOPE";     
}}

submitButton.addEventListener('click', function(){
  var firstNum = document.getElementById("firstNum").value;
  var secondNum = document.getElementById("secondNum").value;
  console.log(firstNum, secondNum);
  document.getElementById("demo").innerHTML = CheckNums(+firstNum, +secondNum);
});