-
Notifications
You must be signed in to change notification settings - Fork 0
/
addition_lvl2.html
349 lines (323 loc) · 13.1 KB
/
addition_lvl2.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Addition Game</title>
<style>
body {
font-family: 'Arial', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background: #f2f2f2;
color: #333;
margin: 0;
height: 100vh;
justify-content: center;
}
h1 {
font-size: 2em;
color: #0073e6;
}
#menu, #game {
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
text-align: center;
}
button {
background: #0073e6;
color: #fff;
border: none;
padding: 10px 20px;
margin: 10px;
border-radius: 5px;
cursor: pointer;
font-size: 1em;
transition: background 0.3s;
}
button:hover {
background: #005bb5;
}
button:disabled {
background: #ccc;
cursor: not-allowed;
}
.hidden {
display: none;
}
.number-box, .carry-box, .input-box {
display: inline-block;
width: 50px;
height: 50px;
line-height: 50px;
margin: 0 5px;
font-size: 1.5em;
font-weight: bold;
color: #0073e6;
text-align: center;
}
.carry-box {
color: #ff4d4d;
}
.highlighted {
background-color: #ffd700;
}
.row {
display: flex;
justify-content: center;
align-items: center;
}
.operator-box {
width: 50px;
height: 50px;
line-height: 50px;
font-size: 1.5em;
font-weight: bold;
color: #0073e6;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
}
.number-selection {
margin-top: 20px;
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.selection-box {
display: inline-block;
width: 50px;
height: 50px;
line-height: 50px;
margin: 5px;
font-size: 1.5em;
font-weight: bold;
color: #0073e6;
text-align: center;
cursor: pointer;
border: 2px solid #0073e6;
border-radius: 5px;
background: #e6f2ff;
transition: background 0.3s, color 0.3s;
}
.selection-box.selected {
background: #0073e6;
color: #fff;
}
.selection-box:hover {
background: #0073e6;
color: #fff;
}
.horizontal-bar {
border-top: 2px solid #333;
width: calc(50px * var(--num-columns) + (var(--num-columns) * 10px));
margin: 0 auto;
}
.feedback {
margin-top: 10px;
font-size: 1.2em;
font-weight: bold;
}
.feedback.success {
color: green;
}
.feedback.error {
color: red;
}
.leading-zero {
visibility: hidden;
}
.leading-zero {
visibility: hidden;
}
</style>
</head>
<body>
<div id="menu">
<h1>Select Number of Digits</h1>
<button onclick="startGame(2)">2 Digits</button>
<button onclick="startGame(3)">3 Digits</button>
<button onclick="startGame(4)">4 Digits</button>
</div>
<div id="game" class="hidden">
<h1>Add the Numbers</h1>
<div class="row">
<div class="operator-box" style="visibility:hidden;">+</div>
<div id="carryRow"></div>
</div>
<div class="row">
<div class="operator-box" style="visibility:hidden;">+</div>
<div id="number1Row"></div>
</div>
<div class="row">
<div class="operator-box">+</div>
<div id="number2Row"></div>
</div>
<div class="horizontal-bar"></div>
<div class="row">
<div class="operator-box" style="visibility:hidden;">+</div>
<div id="resultRow"></div>
</div>
<div class="number-selection">
<div class="selection-box" id="select-1x" onclick="selectDigit('1x')">1x</div>
<div class="selection-box" onclick="selectDigit(0)">0</div>
<div class="selection-box" onclick="selectDigit(1)">1</div>
<div class="selection-box" onclick="selectDigit(2)">2</div>
<div class="selection-box" onclick="selectDigit(3)">3</div>
<div class="selection-box" onclick="selectDigit(4)">4</div>
<div class="selection-box" onclick="selectDigit(5)">5</div>
<div class="selection-box" onclick="selectDigit(6)">6</div>
<div class="selection-box" onclick="selectDigit(7)">7</div>
<div class="selection-box" onclick="selectDigit(8)">8</div>
<div class="selection-box" onclick="selectDigit(9)">9</div>
</div>
<button id="validateBtn" onclick="validateSelection()" disabled>Validate</button>
<div id="feedback" class="feedback hidden"></div>
</div>
<script>
let numDigits;
let number1;
let number2;
let currentDigitIndex;
let carry;
let selectedDigit = null;
let selected1x = false;
function startGame(digits) {
numDigits = digits;
number1 = generateRandomNumber(numDigits);
number2 = generateRandomNumber(numDigits);
let maxResultDigits = Math.max(number1.toString().length, number2.toString().length);
let sumDigits = (number1 + number2).toString().length;
maxResultDigits = Math.max(maxResultDigits, sumDigits);
document.documentElement.style.setProperty('--num-columns', maxResultDigits + 1);
document.getElementById('menu').classList.add('hidden');
document.getElementById('game').classList.remove('hidden');
displayNumbers(maxResultDigits);
currentDigitIndex = maxResultDigits - 1;
carry = Array(maxResultDigits + 1).fill(0);
highlightCurrentColumn();
}
function generateRandomNumber(digits) {
let min = Math.pow(10, digits - 1);
let max = Math.pow(10, digits) - 1;
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function displayNumbers(maxDigits) {
let number1Row = document.getElementById('number1Row');
let number2Row = document.getElementById('number2Row');
let carryRow = document.getElementById('carryRow');
let resultRow = document.getElementById('resultRow');
number1Row.innerHTML = '';
number2Row.innerHTML = '';
carryRow.innerHTML = '';
resultRow.innerHTML = '';
let num1Str = number1.toString().padStart(maxDigits, '0');
let num2Str = number2.toString().padStart(maxDigits, '0');
// For leading zeros detection
let num1Started = false;
let num2Started = false;
// Add an extra carry box on the left
carryRow.innerHTML += `<div class="carry-box" id="carryExtra"></div>`;
for (let i = 0; i < maxDigits; i++) {
carryRow.innerHTML += `<div class="carry-box" id="carry${i}"></div>`;
}
// Similarly, add an extra number box on the left to align
number1Row.innerHTML += `<div class="number-box leading-zero"></div>`;
number2Row.innerHTML += `<div class="number-box leading-zero"></div>`;
resultRow.innerHTML += `<div class="input-box leading-zero"></div>`;
for (let i = 0; i < maxDigits; i++) {
let num1Digit = num1Str.charAt(i);
let num2Digit = num2Str.charAt(i);
if (num1Digit !== '0' || num1Started || i === maxDigits - 1) {
num1Started = true;
number1Row.innerHTML += `<div class="number-box" id="num1-${i}">${num1Digit}</div>`;
} else {
number1Row.innerHTML += `<div class="number-box leading-zero" id="num1-${i}">${num1Digit}</div>`;
}
if (num2Digit !== '0' || num2Started || i === maxDigits - 1) {
num2Started = true;
number2Row.innerHTML += `<div class="number-box" id="num2-${i}">${num2Digit}</div>`;
} else {
number2Row.innerHTML += `<div class="number-box leading-zero" id="num2-${i}">${num2Digit}</div>`;
}
resultRow.innerHTML += `<div class="input-box" id="result${i}">_</div>`;
}
}
function highlightCurrentColumn() {
document.querySelectorAll('.number-box, .carry-box, .input-box').forEach(box => box.classList.remove('highlighted'));
if (currentDigitIndex >= 0) {
// document.getElementById(`carry${currentDigitIndex}`).classList.add('highlighted');
// document.getElementById(`num1-${currentDigitIndex}`).classList.add('highlighted');
// document.getElementById(`num2-${currentDigitIndex}`).classList.add('highlighted');
document.getElementById(`result${currentDigitIndex}`).classList.add('highlighted');
}
}
function selectDigit(value) {
if (value === '1x') {
selected1x = !selected1x;
document.getElementById('select-1x').classList.toggle('selected', selected1x);
return;
}
selectedDigit = value;
document.querySelectorAll('.selection-box').forEach(box => box.classList.remove('selected'));
event.target.classList.add('selected');
if (selected1x) {
document.getElementById('select-1x').classList.add('selected');
}
document.getElementById('validateBtn').disabled = false;
}
function validateSelection() {
if (selectedDigit === null) return;
let selectedValue = parseInt(selectedDigit);
if (isNaN(selectedValue)) return;
let num1Digit = parseInt(document.getElementById(`num1-${currentDigitIndex}`).innerText) || 0;
let num2Digit = parseInt(document.getElementById(`num2-${currentDigitIndex}`).innerText) || 0;
let carryIn = carry[currentDigitIndex] || 0;
let sum = num1Digit + num2Digit + carryIn;
let expectedDigit = sum % 10;
let expectedCarryOut = Math.floor(sum / 10);
let userCarryOut = selected1x ? 1 : 0;
if (selectedValue === expectedDigit && userCarryOut === expectedCarryOut) {
// Correct
document.getElementById(`result${currentDigitIndex}`).innerText = selectedValue;
if (expectedCarryOut > 0) {
carry[currentDigitIndex - 1] = expectedCarryOut;
if (currentDigitIndex - 1 >= 0) {
document.getElementById(`carry${currentDigitIndex - 1}`).innerText = expectedCarryOut;
} else {
document.getElementById(`carryExtra`).innerText = expectedCarryOut;
}
}
document.getElementById('feedback').innerText = "Correct!";
document.getElementById('feedback').classList.remove('hidden', 'error');
document.getElementById('feedback').classList.add('success');
currentDigitIndex--;
if (currentDigitIndex < 0) {
alert('You have completed the addition!');
document.querySelectorAll('.selection-box').forEach(box => box.style.pointerEvents = 'none');
document.getElementById('validateBtn').disabled = true;
} else {
highlightCurrentColumn();
}
// Reset selections
selectedDigit = null;
selected1x = false;
document.querySelectorAll('.selection-box').forEach(box => box.classList.remove('selected'));
document.getElementById('validateBtn').disabled = true;
} else {
// Incorrect
document.getElementById('feedback').innerText = "Incorrect! Try again.";
document.getElementById('feedback').classList.remove('hidden', 'success');
document.getElementById('feedback').classList.add('error');
selectedDigit = null;
selected1x = false;
document.querySelectorAll('.selection-box').forEach(box => box.classList.remove('selected'));
document.getElementById('validateBtn').disabled = true;
}
}
</script>
</body>
</html>