-5
我需要這樣做,而不使用parseInt,但我不知道該怎麼做。我試圖根據a * b的實際值來檢查答案。我認爲有什麼不對,我實際上無法檢索文本框的值。什麼是最好的方式來做到這一點?javascript - if/else永不返回true?
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()">
'answer'是一個DOM節點,'a'和'b'是數字。 – zzzzBov
也許你錯過了什麼? 'answer.value' ... – NewToJS
如果你只是做了'console.log(答案)',你會注意到這個問題。 – Barmar