正解を 2 回入力した後は、正解をもう一度入力しても、それ以降の試行では「再試行」の出力が表示されます。この奇妙な動作の原因は何ですか?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var answer;
var firstNum;
var secondNum;
var check = true;
question();
function question() {
firstNum = Math.floor(Math.random() * 101);
secondNum = Math.floor(Math.random() * 101);
answer = firstNum * secondNum;
document.writeln("<h1>How much is " + firstNum + " times " + secondNum + "</h1>");
document.writeln(answer);
document.write('<input name="inputAnswer" type="text" id="inputAns"/>');
document.write('<input name="submitBtn" type="submit" value="Submit Answer" onclick="validate()"/>');
}
function validate() {
if (document.getElementById("inputAns").value == answer) {
document.write("Good");
check = true;
result();
} else {
check = false;
document.write("try again");
result();
}
}
function result() {
if (check == true)
question();
else {
document.writeln("<h1>How much is " + firstNum + " times " + secondNum + "</h1>");
document.writeln(answer);
document.write('<input name="inputAnswer" type="text" id="inputAns"/>');
document.write('<input name="submitBtn" type="submit" value="Submit Answer" onclick="validate()"/>');
}
}
</script>
</head>
<body></body>
</html>