-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiplication-0-10.js
More file actions
132 lines (109 loc) · 3.87 KB
/
multiplication-0-10.js
File metadata and controls
132 lines (109 loc) · 3.87 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
import { animationSystem } from './animations.js';
import { setupScratchpad, shuffleArray } from './shared.js';
let currentProblemData = null;
let problemHistory = [];
let currentHistoryIndex = -1;
const problemTextElement = document.getElementById('problem-text');
const optionsContainer = document.getElementById('options-container');
const prevButton = document.getElementById('prev-problem');
const nextButton = document.getElementById('next-problem');
function generateOptions(correctAnswer) {
const maxProduct = 100;
const options = new Set([correctAnswer]);
while (options.size < 4) {
const offset = Math.floor(Math.random() * 21) - 10;
const candidate = correctAnswer + offset;
if (candidate >= 0 && candidate <= maxProduct) {
options.add(candidate);
}
}
return shuffleArray(Array.from(options));
}
function generateProblem() {
const minFactor = 0;
const maxFactor = 10;
const num1 = Math.floor(Math.random() * (maxFactor - minFactor + 1)) + minFactor;
const num2 = Math.floor(Math.random() * (maxFactor - minFactor + 1)) + minFactor;
const correctAnswer = num1 * num2;
return {
questionText: `${num1} × ${num2} = ?`,
correctAnswer,
options: generateOptions(correctAnswer),
};
}
function setNavigationState() {
prevButton.disabled = currentHistoryIndex <= 0;
nextButton.disabled = false;
}
function displayProblem(problemData) {
currentProblemData = problemData;
problemTextElement.textContent = currentProblemData.questionText;
optionsContainer.innerHTML = '';
for (const optionValue of currentProblemData.options) {
const button = document.createElement('button');
button.className = 'option';
button.textContent = String(optionValue);
button.dataset.value = String(optionValue);
optionsContainer.appendChild(button);
}
setNavigationState();
}
function loadProblemFromHistory(index) {
if (index < 0 || index >= problemHistory.length) {
return;
}
currentHistoryIndex = index;
displayProblem(problemHistory[currentHistoryIndex]);
}
function generateAndShowNewProblem() {
const newProblem = generateProblem();
problemHistory.push(newProblem);
currentHistoryIndex = problemHistory.length - 1;
displayProblem(newProblem);
}
function handleOptionClick(selectedButton) {
if (!currentProblemData || selectedButton.disabled) {
return;
}
const selectedValue = Number(selectedButton.dataset.value);
const allOptions = optionsContainer.querySelectorAll('.option');
allOptions.forEach((option) => {
option.disabled = true;
});
const isCorrect = selectedValue === currentProblemData.correctAnswer;
if (isCorrect) {
animationSystem.handleCorrectAnswer(selectedButton, allOptions, () => {
generateAndShowNewProblem();
});
} else {
animationSystem.handleWrongAnswer(selectedButton);
setTimeout(() => {
allOptions.forEach((option) => {
option.disabled = false;
});
}, 550);
}
}
document.addEventListener('DOMContentLoaded', () => {
setupScratchpad();
optionsContainer.addEventListener('click', (event) => {
const target = event.target;
if (!(target instanceof HTMLButtonElement) || !target.classList.contains('option')) {
return;
}
handleOptionClick(target);
});
prevButton.addEventListener('click', () => {
if (currentHistoryIndex > 0) {
loadProblemFromHistory(currentHistoryIndex - 1);
}
});
nextButton.addEventListener('click', () => {
if (currentHistoryIndex < problemHistory.length - 1) {
loadProblemFromHistory(currentHistoryIndex + 1);
return;
}
generateAndShowNewProblem();
});
generateAndShowNewProblem();
});