<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Math Quiz</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
color: #333;
text-align: center;
}
h1 {
color: #4CAF50;
}
input {
padding: 8px;
font-size: 18px;
margin: 10px 0;
width: 100px;
text-align: center;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
}
button:hover {
background-color: #45a049;
}
#feedback {
margin-top: 10px;
font-size: 18px;
}
.correct {
color: green;
}
.incorrect {
color: red;
}
#question {
font-size: 24px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Math Quiz</h1>
<p id="question">1 + 1 = ?</p>
<input id="answer" type="number" placeholder="Your answer" />
<button onclick="submitAnswer()">Submit</button>
<p id="feedback"></p>
<script>
let correctAnswer = 2; // Initial correct answer for 1+1
function generate(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function submitAnswer() {
let userAnswer = parseFloat(document.getElementById("answer").value);
let feedback = document.getElementById("feedback");
if (userAnswer === correctAnswer) {
feedback.innerHTML = "✅ Correct! Next question:";
feedback.className = "correct"; // Feedback style for correct answer
let operationType = generate(1, 4);
let num1 = generate(1, 100);
let num2 = generate(1, 100);
if (operationType === 1) {
correctAnswer = num1 + num2;
document.getElementById("question").innerHTML = `${num1} + ${num2} = ?`;
} else if (operationType === 2) {
correctAnswer = num1 - num2;
document.getElementById("question").innerHTML = `${num1} - ${num2} = ?`;
} else if (operationType === 3) {
correctAnswer = parseFloat((num1 / num2).toFixed(2));
document.getElementById("question").innerHTML = `${num1} ÷ ${num2} = ? (Round to 2 decimal places)`;
} else {
correctAnswer = num1 * num2;
document.getElementById("question").innerHTML = `${num1} × ${num2} = ?`;
}
document.getElementById("answer").value = ""; // Clear the input field
} else {
feedback.innerHTML = "❌ Incorrect, try again!";
feedback.className = "incorrect"; // Feedback style for incorrect answer
}
}
</script>
</body>
</html>