如果/其他永远不会返回真实



我需要在不使用parseint的情况下执行此操作,但我不确定该怎么做。我正在尝试根据A * B的实际值检查答案。我认为出了什么问题是我实际上无法检索文本框的值。最好的方法是什么?

var answer = document.getElementById('answer');
var a = Math.floor((Math.random() * 12) + 1);
var b = Math.floor((Math.random() * 12) + 1);
document.getElementById('question').innerHTML = "What is " + a + " times " + b + "?";
function wrongRight() {
  if (a * b == answer) {
    document.getElementById('response').innerHTML = "Very good.";
  } else {
    document.getElementById('response').innerHTML = "No, please try again.";
  }
}
<p id="response"></p>
<p id="question"></p>
<!-- this is where the x * x = ? will be asked-->
<p> your answer </p>
<input type="text" id="answer">
<input type="button" value="Click" onClick="wrongRight()">

是的,如评论中所述,您将数字与DOM节点进行了比较。在您的功能中,wrongRight+answer.value替换answer。加号将其从字符串转换为整数。例如

function wrongRight() {
    if(a * b == +answer.value) {
        document.getElementById('response').innerHTML = "Very good.";
    } else{
        document.getElementById('response').innerHTML = "No, please try again.";
    }
}

最新更新