-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath.html
More file actions
253 lines (219 loc) · 8.19 KB
/
Math.html
File metadata and controls
253 lines (219 loc) · 8.19 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Math Game</title>
<!-- Google Font -->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@500;600&display=swap" rel="stylesheet" />
<!-- Custom Styles -->
<style>
body {
background: linear-gradient(135deg, rgba(52, 152, 219, 0.8), rgba(155, 89, 182, 0.8));
font-family: "Roboto", sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
overflow: hidden;
}
.container {
background: rgba(255, 255, 255, 0.1);
border-radius: 20px;
padding: 2em;
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(10px);
max-width: 400px;
width: 100%;
text-align: center;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.container:hover {
transform: scale(1.05);
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
h3 {
font-size: 2.2em;
color: #080808;
margin-bottom: 1em;
font-weight: 600;
}
#question {
font-size: 2.5em;
font-weight: 600;
color: #ffffff;
margin-bottom: 1.5em;
}
input {
font-size: 1.2em;
width: 109px;
padding: 0.8em;
text-align: center;
border: 2px solid #3498db;
border-radius: 8px;
background: rgba(255, 255, 255, 0.2);
color: #fff;
margin-bottom: 1.5em;
outline: none;
transition: border 0.3s ease, box-shadow 0.3s ease;
position: relative;
left: 92px;
top: -36px;
}
input:focus {
border-color: #8e44ad;
box-shadow: 0 0 10px rgba(142, 68, 173, 0.7);
}
.btn-custom {
font-size: 1.3em;
font-weight: 600;
padding: 0.8em 2.5em;
background-color: #0a314b;
color: #fff;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.3s ease;
position: relative;
top: 25 px;
left: 4px;
}
.btn-custom:hover {
background-color: #2980b9;
transform: translateY(-3px);
}
.btn-custom:disabled {
background-color: #bdc3c7;
cursor: not-allowed;
}
.result-message {
font-size: 1.6em;
color: #ffffff;
margin-top: 1em;
opacity: 0;
transition: opacity 0.5s ease;
}
#error-msg {
color: #e74c3c;
margin-top: 3em;
font-size: 1.2em;
display: none;
}
.hide {
display: none;
}
.highlight {
color: #8e44ad;
font-weight: bold;
}
@media (max-width: 600px) {
.container {
padding: 2em;
}
h3 {
font-size: 1.8em;
}
#question {
font-size: 2.2em;
}
input {
width: 100px;
font-size: 1.4em;
}
.btn-custom {
padding: 0.6em 2em;
}
}
</style>
</head>
<body>
<div class="container">
<h3>Choose the Correct Number Or Operator</h3>
<div id="question"></div>
<input type="text" id="inputValue" placeholder="?" class="form-control hide" />
<button id="submit-btn" class="btn btn-custom hide"
style="position: relative; top:60px; left:-px">Submit</button>
<p id="error-msg">Please fill in a valid answer.</p>
<div class="result-message" id="result"></div>
<button id="start-btn" class="btn btn-custom">Start Game</button>
</div>
<script>
const operators = ["+", "-", "*"];
const startBtn = document.getElementById("start-btn");
const question = document.getElementById("question");
const submitBtn = document.getElementById("submit-btn");
const errorMessage = document.getElementById("error-msg");
const resultMessage = document.getElementById("result");
const inputField = document.getElementById("inputValue");
let answerValue;
let operatorQuestion;
let questionText;
const randomValue = (min, max) => Math.floor(Math.random() * (max - min)) + min;
const questionGenerator = () => {
let [num1, num2] = [randomValue(1, 20), randomValue(1, 20)];
let randomOperator = operators[Math.floor(Math.random() * operators.length)];
// Avoid negative results for subtraction (if num2 is greater than num1)
if (randomOperator == "-" && num2 > num1) {
[num1, num2] = [num2, num1];
}
let solution = eval(`${num1}${randomOperator}${num2}`);
let randomVar = randomValue(1, 5); // Randomly select which part is missing
// Handling each case where "?" should appear
if (randomVar == 1) {
answerValue = num1;
questionText = `? ${randomOperator} ${num2} = ${solution}`; // Missing number 1
} else if (randomVar == 2) {
answerValue = num2;
questionText = `${num1} ${randomOperator} ? = ${solution}`; // Missing number 2
} else if (randomVar == 3) {
answerValue = randomOperator;
operatorQuestion = true;
questionText = `${num1} ? ${num2} = ${solution}`; // Missing operator
} else {
answerValue = solution;
questionText = `${num1} ${randomOperator} ${num2} = ?`; // Missing result
}
// Ensure the question is updated with the question text correctly
question.innerHTML = questionText;
};
startBtn.addEventListener("click", () => {
resultMessage.textContent = "";
errorMessage.style.display = "none";
startBtn.style.display = "none";
submitBtn.classList.remove("hide");
inputField.classList.remove("hide");
questionGenerator();
});
submitBtn.addEventListener("click", () => {
let userInput = inputField.value.trim();
errorMessage.style.display = "none";
resultMessage.style.opacity = 1;
if (userInput) {
if (userInput == answerValue) {
resultMessage.innerHTML = `Yippie!! <span class="text-success">Correct</span> Answer`;
// Replace the '?' with the correct answer in the question
let updatedQuestion = questionText.replace('?', `<span class="highlight">${userInput}</span>`);
question.innerHTML = updatedQuestion;
} else if (operatorQuestion && !operators.includes(userInput)) {
errorMessage.textContent = "Please enter a valid operator (+, -, *)";
errorMessage.style.display = "block";
resultMessage.innerHTML = `Oops!! <span class="text-danger">Wrong</span> Answer`;
} else {
resultMessage.innerHTML = `Oops!! <span class="text-danger">Wrong</span> Answer`;
}
inputField.value = '';
setTimeout(() => {
resultMessage.style.opacity = 0;
errorMessage.style.display = "none";
inputField.classList.add("hide");
submitBtn.classList.add("hide");
startBtn.style.display = "inline-block";
}, 1500);
} else {
errorMessage.textContent = "Input Cannot Be Empty";
errorMessage.style.display = "block";
}
});
</script>
</body>
</html>