-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
122 lines (107 loc) · 3.03 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Math Game</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
overflow: hidden;
}
.wrapper {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
text-align: center;
background-color: white;
border-radius: 10px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 36px;
margin-bottom: 30px;
color: #333;
}
#question {
font-size: 48px;
margin-bottom: 30px;
color: #333;
}
#answers {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin-bottom: 30px;
}
button {
width: 150px;
height: 80px;
margin: 10px;
font-size: 24px;
font-weight: bold;
color: white;
background-color: #333;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.2s ease;
}
button:hover {
background-color: #555;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container">
<h1>Math Game</h1>
<div id="question"></div>
<div id="answers"></div>
</div>
</div>
<audio id="correctAudio" src="correct.mp3"></audio>
<audio id="incorrectAudio" src="incorrect.mp3"></audio>
<script>
var questionElement = document.getElementById('question');
var answersElement = document.getElementById('answers');
function generateQuestion() {
var num1 = Math.floor(Math.random() * 10) + 1;
var num2 = Math.floor(Math.random() * 10) + 1;
var operator = Math.random() < 0.5 ? '+' : '-';
var answer = operator === '+' ? num1 + num2 : num1 - num2;
questionElement.textContent = num1 + ' ' + operator + ' ' + num2 + ' = ?';
var answerOptions = [answer];
while (answerOptions.length < 4) {
var option = Math.floor(Math.random() * 20) + 1;
if (answerOptions.indexOf(option) === -1) {
answerOptions.push(option);
}
}
answersElement.innerHTML = '';
answerOptions.sort(function() { return 0.5 - Math.random(); });
for (var i = 0; i < answerOptions.length; i++) {
var answerButton = document.createElement('button');
answerButton.textContent = answerOptions[i];
answerButton.onclick = function() {
if (this.textContent == answer) {
document.getElementById('correctAudio').play();
} else {
document.getElementById('incorrectAudio').play();
}
generateQuestion();
}
answersElement.appendChild(answerButton);
}
}
generateQuestion();
</script>
</body>
</html>