-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomary-units-up-to-100.js
513 lines (440 loc) · 21.4 KB
/
customary-units-up-to-100.js
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Problem generator functions
function generateProblem() {
// Names array to cycle through
const names = ['Elizabeth', 'Matilda', 'Dylan', 'Joe', 'Andrew', 'James', 'Sarah', 'Austin',
'Angela', 'Sydney', 'Ashley', 'Sadie', 'Adalyn', 'Michael', 'Emma', 'Noah',
'Olivia', 'William', 'Ava', 'Benjamin', 'Isabella', 'Lucas', 'Mia', 'Micah',
'Charlotte', 'Theodore', 'Amelia', 'Jack', 'Harper', 'Oliver'];
// Helper function for unit pluralization
function getUnit(value, unit) {
if (unit === 'foot') {
return value === 1 ? 'foot' : 'feet';
}
return value === 1 ? unit : unit + 's';
}
// Get two different random names
let name1Index = Math.floor(Math.random() * names.length);
let name2Index;
do {
name2Index = Math.floor(Math.random() * names.length);
} while (name2Index === name1Index);
const name1 = names[name1Index];
const name2 = names[name2Index];
// Generate appropriate numbers based on the measurement context
const smallNumber = Math.floor(Math.random() * 5) + 1; // 1-5
const mediumNumber = Math.floor(Math.random() * 15) + 5; // 5-20
const largeNumber = Math.floor(Math.random() * 25) + 15; // 15-40
// Helper function to ensure sums don't exceed 100
function getValidSecondNumber(firstNum, maxSum = 100) {
const maxAllowed = maxSum - firstNum;
return Math.min(smallNumber, maxAllowed);
}
const problemTypes = [
// Tree and plant measurements (in feet)
{
text: `There are two trees in ${name1}'s backyard. One is ${mediumNumber} ${getUnit(mediumNumber, 'foot')} tall, and the pine tree is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'foot')} taller. How tall is the pine tree?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1}'s rose bush is ${smallNumber} ${getUnit(smallNumber, 'foot')} tall. ${name2}'s rose bush is ${getValidSecondNumber(smallNumber)} ${getUnit(getValidSecondNumber(smallNumber), 'foot')} taller. How tall is ${name2}'s rose bush?`,
answer: smallNumber + getValidSecondNumber(smallNumber)
},
// Indoor measurements (in feet)
{
text: `${name1} has a rug that is ${mediumNumber} ${getUnit(mediumNumber, 'foot')} long. ${name2}'s rug is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'foot')} longer. How long is ${name2}'s rug?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1}'s hallway is ${largeNumber} ${getUnit(largeNumber, 'foot')} long. ${name2}'s hallway is ${smallNumber} ${getUnit(smallNumber, 'foot')} shorter. How long is ${name2}'s hallway?`,
answer: largeNumber - smallNumber
},
// Playground measurements (in feet)
{
text: `${name1} drew a hopscotch court ${mediumNumber} ${getUnit(mediumNumber, 'foot')} long. ${name2} drew one ${smallNumber} ${getUnit(smallNumber, 'foot')} shorter. How long is ${name2}'s hopscotch court?`,
answer: mediumNumber - smallNumber
},
{
text: `${name1}'s jump rope is ${largeNumber} ${getUnit(largeNumber, 'foot')} long. ${name2}'s jump rope is ${smallNumber} ${getUnit(smallNumber, 'foot')} shorter. How long is ${name2}'s jump rope?`,
answer: largeNumber - smallNumber
},
// Toy measurements (in inches)
{
text: `At the toy store, ${name1} found a toy car ${mediumNumber} ${getUnit(mediumNumber, 'inch')} long. ${name2} found one that is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'inch')} longer. How long is ${name2}'s toy car?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1}'s model airplane is ${largeNumber} ${getUnit(largeNumber, 'inch')} long. ${name2}'s is ${smallNumber} ${getUnit(smallNumber, 'inch')} shorter. How long is ${name2}'s model airplane?`,
answer: largeNumber - smallNumber
},
// Yard measurements (in yards)
{
text: `${name1}'s garden is ${mediumNumber} ${getUnit(mediumNumber, 'yard')} long. ${name2}'s garden is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'yard')} longer. How long is ${name2}'s garden?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1}'s driveway is ${largeNumber} ${getUnit(largeNumber, 'yard')} long. ${name2}'s driveway is ${smallNumber} ${getUnit(smallNumber, 'yard')} shorter. How long is ${name2}'s driveway?`,
answer: largeNumber - smallNumber
},
// Sports field measurements (in yards)
{
text: `${name1} can throw a football ${mediumNumber} ${getUnit(mediumNumber, 'yard')}. ${name2} can throw it ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'yard')} further. How far can ${name2} throw the football?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1} ran ${largeNumber} ${getUnit(largeNumber, 'yard')} in practice. ${name2} ran ${smallNumber} ${getUnit(smallNumber, 'yard')} less. How far did ${name2} run?`,
answer: largeNumber - smallNumber
},
// Craft projects (in inches)
{
text: `${name1}'s paper chain is ${mediumNumber} ${getUnit(mediumNumber, 'inch')} long. ${name2}'s is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'inch')} longer. How long is ${name2}'s paper chain?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1} made a bookmark ${smallNumber} ${getUnit(smallNumber, 'inch')} long. ${name2}'s bookmark is ${getValidSecondNumber(smallNumber)} ${getUnit(getValidSecondNumber(smallNumber), 'inch')} longer. How long is ${name2}'s bookmark?`,
answer: smallNumber + getValidSecondNumber(smallNumber)
},
// Building measurements (in feet)
{
text: `${name1}'s treehouse is ${mediumNumber} ${getUnit(mediumNumber, 'foot')} tall. ${name2}'s treehouse is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'foot')} taller. How tall is ${name2}'s treehouse?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1}'s fence is ${largeNumber} ${getUnit(largeNumber, 'foot')} long. ${name2}'s fence is ${smallNumber} ${getUnit(smallNumber, 'foot')} shorter. How long is ${name2}'s fence?`,
answer: largeNumber - smallNumber
},
// Ribbon and string measurements (in inches)
{
text: `${name1} needs ${mediumNumber} ${getUnit(mediumNumber, 'inch')} of ribbon. ${name2} needs ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'inch')} more. How many inches of ribbon does ${name2} need?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1} has ${mediumNumber} ${getUnit(mediumNumber, 'inch')} of string. ${name2} has ${smallNumber} ${getUnit(smallNumber, 'inch')} less. How many inches of string does ${name2} have?`,
answer: mediumNumber - smallNumber
},
// Path and trail measurements (in yards)
{
text: `${name1}'s walking path is ${mediumNumber} ${getUnit(mediumNumber, 'yard')} long. ${name2}'s is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'yard')} longer. How long is ${name2}'s path?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1}'s nature trail is ${largeNumber} ${getUnit(largeNumber, 'yard')} long. ${name2}'s trail is ${smallNumber} ${getUnit(smallNumber, 'yard')} shorter. How long is ${name2}'s trail?`,
answer: largeNumber - smallNumber
},
// School supplies (in inches)
{
text: `${name1}'s ruler is ${smallNumber} ${getUnit(smallNumber, 'inch')} long. ${name2}'s ruler is ${getValidSecondNumber(smallNumber)} ${getUnit(getValidSecondNumber(smallNumber), 'inch')} longer. How long is ${name2}'s ruler?`,
answer: smallNumber + getValidSecondNumber(smallNumber)
},
{
text: `${name1}'s pencil box is ${mediumNumber} ${getUnit(mediumNumber, 'inch')} long. ${name2}'s is ${smallNumber} ${getUnit(smallNumber, 'inch')} shorter. How long is ${name2}'s pencil box?`,
answer: mediumNumber - smallNumber
},
// Garden features (in feet)
{
text: `${name1}'s garden bed is ${mediumNumber} ${getUnit(mediumNumber, 'foot')} long. ${name2}'s is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'foot')} longer. How long is ${name2}'s garden bed?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1}'s row of flowers is ${mediumNumber} ${getUnit(mediumNumber, 'foot')} long. ${name2}'s row is ${smallNumber} ${getUnit(smallNumber, 'foot')} shorter. How long is ${name2}'s row of flowers?`,
answer: mediumNumber - smallNumber
},
// Playground equipment (in feet)
{
text: `${name1}'s slide is ${mediumNumber} ${getUnit(mediumNumber, 'foot')} long. ${name2}'s slide is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'foot')} longer. How long is ${name2}'s slide?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1}'s monkey bars are ${largeNumber} ${getUnit(largeNumber, 'foot')} long. ${name2}'s are ${smallNumber} ${getUnit(smallNumber, 'foot')} shorter. How long are ${name2}'s monkey bars?`,
answer: largeNumber - smallNumber
},
// Sports equipment (in inches)
{
text: `${name1}'s baseball bat is ${mediumNumber} ${getUnit(mediumNumber, 'inch')} long. ${name2}'s bat is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'inch')} longer. How long is ${name2}'s bat?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
{
text: `${name1}'s hockey stick is ${largeNumber} ${getUnit(largeNumber, 'inch')} long. ${name2}'s is ${smallNumber} ${getUnit(smallNumber, 'inch')} shorter. How long is ${name2}'s hockey stick?`,
answer: largeNumber - smallNumber
},
// Art supplies (in inches)
{
text: `${name1}'s paintbrush is ${smallNumber} ${getUnit(smallNumber, 'inch')} long. ${name2}'s paintbrush is ${getValidSecondNumber(smallNumber)} ${getUnit(getValidSecondNumber(smallNumber), 'inch')} longer. How long is ${name2}'s paintbrush?`,
answer: smallNumber + getValidSecondNumber(smallNumber)
},
{
text: `${name1}'s canvas is ${mediumNumber} ${getUnit(mediumNumber, 'inch')} wide. ${name2}'s canvas is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'inch')} wider. How wide is ${name2}'s canvas?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
},
// Building blocks (in inches)
{
text: `${name1}'s tower of blocks is ${mediumNumber} ${getUnit(mediumNumber, 'inch')} tall. ${name2}'s tower is ${getValidSecondNumber(mediumNumber)} ${getUnit(getValidSecondNumber(mediumNumber), 'inch')} taller. How tall is ${name2}'s tower?`,
answer: mediumNumber + getValidSecondNumber(mediumNumber)
}
];
// Select a random problem type
const selectedProblem = problemTypes[Math.floor(Math.random() * problemTypes.length)];
const answer = selectedProblem.answer;
// Generate wrong answers that are close to the correct answer but still reasonable
function generateUniqueWrongAnswer(correctAnswer, existingAnswers) {
let attempt = 0;
let wrongAnswer;
do {
// Increase the range of wrong answers if we're having trouble finding unique ones
const range = attempt < 5 ? 5 : 10;
const offset = Math.random() < 0.5 ?
Math.floor(Math.random() * range) + 1 :
-(Math.floor(Math.random() * range) + 1);
wrongAnswer = correctAnswer + offset;
attempt++;
} while ((existingAnswers.includes(wrongAnswer) || wrongAnswer <= 0 || wrongAnswer > 100) && attempt < 20);
return wrongAnswer;
}
// Generate three unique wrong answers
const wrongAnswers = [];
for (let i = 0; i < 3; i++) {
const wrongAnswer = generateUniqueWrongAnswer(answer, [...wrongAnswers, answer]);
if (wrongAnswer > 0 && wrongAnswer <= 100 && !wrongAnswers.includes(wrongAnswer) && wrongAnswer !== answer) {
wrongAnswers.push(wrongAnswer);
}
}
// If we couldn't generate enough unique wrong answers, fill in with more distant numbers
while (wrongAnswers.length < 3) {
const offset = wrongAnswers.length * 5 + 5;
const wrongAnswer = answer + offset;
if (wrongAnswer <= 100 && !wrongAnswers.includes(wrongAnswer) && wrongAnswer !== answer) {
wrongAnswers.push(wrongAnswer);
}
}
return {
text: selectedProblem.text,
answer: answer,
options: shuffleArray([answer, ...wrongAnswers])
};
}
// Scratchpad functionality
class Scratchpad {
constructor(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.isDrawing = false;
this.paths = [];
this.redoPaths = [];
this.currentPath = [];
// Bind methods to this
this.startDrawing = this.startDrawing.bind(this);
this.draw = this.draw.bind(this);
this.stopDrawing = this.stopDrawing.bind(this);
this.setCanvasSize = this.setCanvasSize.bind(this);
// Setup event listeners
this.setupEventListeners();
// Handle window resize
window.addEventListener('resize', this.setCanvasSize);
}
initialize() {
// Set canvas size
const rect = this.canvas.getBoundingClientRect();
this.canvas.width = rect.width;
this.canvas.height = 500;
// Initial canvas setup
this.ctx = this.canvas.getContext('2d');
this.ctx.strokeStyle = '#000000';
this.ctx.lineWidth = 3;
this.ctx.lineCap = 'round';
this.ctx.lineJoin = 'round';
}
setCanvasSize() {
const rect = this.canvas.getBoundingClientRect();
this.canvas.width = rect.width;
this.canvas.height = 500;
// Reset context properties after resize
this.ctx.strokeStyle = '#000000';
this.ctx.lineWidth = 3;
this.ctx.lineCap = 'round';
this.ctx.lineJoin = 'round';
this.redraw();
}
setupEventListeners() {
// Mouse events
this.canvas.addEventListener('mousedown', this.startDrawing);
this.canvas.addEventListener('mousemove', this.draw);
this.canvas.addEventListener('mouseup', this.stopDrawing);
this.canvas.addEventListener('mouseleave', this.stopDrawing);
// Touch events
this.canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
const touch = e.touches[0];
const rect = this.canvas.getBoundingClientRect();
this.startDrawing({
clientX: touch.clientX,
clientY: touch.clientY
});
});
this.canvas.addEventListener('touchmove', (e) => {
e.preventDefault();
const touch = e.touches[0];
this.draw({
clientX: touch.clientX,
clientY: touch.clientY
});
});
this.canvas.addEventListener('touchend', this.stopDrawing);
}
getPoint(e) {
const rect = this.canvas.getBoundingClientRect();
return {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
}
startDrawing(e) {
this.isDrawing = true;
const point = this.getPoint(e);
this.currentPath = [point];
this.ctx.beginPath();
this.ctx.moveTo(point.x, point.y);
}
draw(e) {
if (!this.isDrawing) return;
const point = this.getPoint(e);
this.currentPath.push(point);
this.ctx.lineTo(point.x, point.y);
this.ctx.stroke();
}
stopDrawing() {
if (!this.isDrawing) return;
this.isDrawing = false;
if (this.currentPath.length > 1) {
this.paths.push([...this.currentPath]);
this.redoPaths = [];
}
this.currentPath = [];
}
redraw() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
// Reset context properties
this.ctx.strokeStyle = '#000000';
this.ctx.lineWidth = 3;
this.ctx.lineCap = 'round';
this.ctx.lineJoin = 'round';
const drawPath = (path) => {
if (path.length < 2) return;
this.ctx.beginPath();
this.ctx.moveTo(path[0].x, path[0].y);
for (let i = 1; i < path.length; i++) {
this.ctx.lineTo(path[i].x, path[i].y);
}
this.ctx.stroke();
};
// Draw all completed paths
this.paths.forEach(drawPath);
}
undo() {
if (this.paths.length > 0) {
this.redoPaths.push(this.paths.pop());
this.redraw();
}
}
redo() {
if (this.redoPaths.length > 0) {
this.paths.push(this.redoPaths.pop());
this.redraw();
}
}
clear() {
this.paths = [];
this.redoPaths = [];
this.currentPath = [];
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
// Utility functions
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
// Game state
let currentProblem = null;
let currentProblemIndex = 0;
const problems = [];
const totalProblems = 50;
// Generate all problems at start
for (let i = 0; i < totalProblems; i++) {
problems.push(generateProblem());
}
// DOM elements
const problemText = document.getElementById('problem-text');
const optionsContainer = document.getElementById('options-container');
const prevButton = document.getElementById('prev-btn');
const nextButton = document.getElementById('next-btn');
// Scratchpad elements
const scratchpadArea = document.getElementById('scratchpad-area');
const scratchpadCanvas = document.getElementById('scratchpad');
const toggleScratchpadBtn = document.getElementById('toggle-scratchpad');
const closeScratchpadBtn = document.getElementById('close-scratchpad');
const undoBtn = document.getElementById('undo-btn');
const redoBtn = document.getElementById('redo-btn');
const clearBtn = document.getElementById('clear-btn');
// Initialize scratchpad
const scratchpad = new Scratchpad(scratchpadCanvas);
// Scratchpad control handlers
toggleScratchpadBtn.onclick = () => {
scratchpadArea.classList.add('open');
toggleScratchpadBtn.style.display = 'none';
// Initialize the scratchpad after the element is visible
requestAnimationFrame(() => {
scratchpad.initialize();
});
};
closeScratchpadBtn.onclick = () => {
scratchpadArea.classList.remove('open');
toggleScratchpadBtn.style.display = 'block';
};
undoBtn.onclick = () => scratchpad.undo();
redoBtn.onclick = () => scratchpad.redo();
clearBtn.onclick = () => scratchpad.clear();
// Event handlers
function handleOptionClick(option, index) {
const selectedAnswer = parseInt(option.textContent);
if (selectedAnswer === currentProblem.answer) {
animationSystem.handleCorrectAnswer(option, optionsContainer.getElementsByClassName('option'), () => {
if (currentProblemIndex < totalProblems - 1) {
currentProblemIndex++;
displayProblem();
}
});
} else {
animationSystem.handleWrongAnswer(option);
}
}
function displayProblem() {
currentProblem = problems[currentProblemIndex];
problemText.textContent = currentProblem.text;
const options = optionsContainer.getElementsByClassName('option');
Array.from(options).forEach((option, index) => {
option.textContent = currentProblem.options[index];
option.className = 'option';
option.disabled = false;
option.onclick = () => handleOptionClick(option, index);
});
// Update navigation buttons
prevButton.disabled = currentProblemIndex === 0;
nextButton.disabled = currentProblemIndex === totalProblems - 1;
}
// Navigation handlers
prevButton.onclick = () => {
if (currentProblemIndex > 0) {
currentProblemIndex--;
displayProblem();
}
};
nextButton.onclick = () => {
if (currentProblemIndex < totalProblems - 1) {
currentProblemIndex++;
displayProblem();
}
};
// Initialize the first problem
displayProblem();