I made a code that has some simple math problems. When the user has pressed submit the if statements would determine whether the text box is null or has some input. I put an if statement for that if the user got the question correct it would document. write correct and if it is null it would write not answered and so on. Instead of that, it outputs correct no matter if the text box is left empty or if they got the answer wrong. I know that document.write is frowned upon but I'm a beginner so please help me.
document.getElementById("q1");
document.getElementById("q2");
document.getElementById("q3");
document.getElementById("q4");
document.getElementById("a1");
document.getElementById("a2");
document.getElementById("a3");
document.getElementById("a4");
document.getElementById("submit");
function check() {
if((q1.value == 6084) || (q1.value == 6,084)){
document.write("<b>" + "1. Correct" + "</b>");
} else if(q1.value.length == 0){
document.write("<b>" + "1. Not Answered" + "</b>");
}
else {
document.write("<b>" + "1. Incorrect" + "</b>");
}
}
<!DOCTYPE html>
<html>
<head>
<title>Quiz1</title>
</head>
<body>
<h1 align = "center">Math Test đ✍</h1>
<h3 id = "q1">Question 1 : 78 * 78 = ?</h3>
<input id = "a1" type = "text" required>
<h3 id = "q2">Question 2 : (100 / 6) + 45 = ?</h3>
<input id = "a2" type = "text">
<h3 id = "q3">Question 3 : 87 + 123 = ?</h3>
<input id = "a3" type = "text">
<h3 id = "q4">Question 4 : 100 - 23 = ?</h3>
<input id = "a4" type = "text">
<br>
<br>
<button id = "submit" onclick = "check()">I'm Done!</button>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire