-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
148 lines (132 loc) · 3.63 KB
/
script.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
const defaultMode = 0;
const modes = [
{
name: "Multiply", symbol: "×",
generate: () => {
let a = random(1, 12);
let b = random(1, 12);
return [a, b, a * b]
}
},
{
name: "Divide", symbol: "÷",
generate: () => {
let a = random(1, 12);
let b = random(1, 12);
return [a * b, a, b];
}
},
{
name: "Add", symbol: "+",
generate: () => {
let a = random(1, 100);
let b = random(1, 100);
return [a, b, a + b]
}
},
{
name: "Subtract", symbol: "−",
generate: () => {
let a = random(1, 100);
let b = random(1, 100);
return [a, b, a - b]
}
}
]
function random(min, max) // [min, max]
{
return min + Math.floor(Math.random() * (max - min + 1));
}
function getModeIndex() {
let url = new URL(document.location);
let modeIndex = parseInt(url.searchParams.get("mode"));
return isNaN(modeIndex) ? defaultMode : modeIndex;
}
function setModeIndex(index) {
let newIndex = Math.min(Math.max(index, 0), modes.length - 1);
// change browser url
let url = new URL(document.location);
url.searchParams.set("mode", newIndex);
history.replaceState(null, "", url.href);
document.title = modes[newIndex].name + "!";
document.getElementById("prev-mode-button").disabled = (newIndex <= 0);
document.getElementById("next-mode-button").disabled = (newIndex >= modes.length - 1);
nextQuestion();
playModeAnimation();
}
function nextQuestion() {
let mode = modes[getModeIndex()];
let [operatorA, operatorB, answer] = mode.generate();
window.answer = answer;
let questionElement = document.getElementById("question");
document.getElementById("prev-question").innerHTML = questionElement.innerHTML;
questionElement.children[0].textContent = operatorA;
questionElement.children[1].textContent = mode.symbol;
questionElement.children[2].textContent = operatorB;
playNextQuestionAnimation();
document.getElementById("answer").focus();
}
function checkAnswer() {
let input = document.getElementById("answer");
if(input.value == window.answer) {
input.value = "";
nextQuestion();
playCorrectAnimation();
} else {
playIncorrectAnimation();
}
input.select();
}
function playModeAnimation() {
anime({
targets: document.getElementById("mode-header-container"),
duration: 450,
left: (-getModeIndex() * 100) + "%",
easing: "easeOutQuint"
});
};
function playNextQuestionAnimation() {
let targets = "#question-container > p";
anime.remove(targets);
anime({
targets: targets,
duration: 450,
translateY: ["-100%", "0%"],
scale: (_el, i, _t) => (i === 0 ? [0.85, 1.0] : [1.0, 0.85]),
easing: "easeOutQuint"
});
}
function playCorrectAnimation() {
anime({
targets: "#answer",
duration: 400,
borderColor: ["#18ad2c", "#d3d3d3"],
easing: "linear"
});
}
function playIncorrectAnimation() {
anime.timeline().add({
targets: "#answer",
duration: 400,
borderColor: ["#ff0000", "#d3d3d3"],
easing: "linear"
}).add({
targets: "#question",
duration: 220,
translateX: ["0em", "-0.1em", "0.1em", "0em"],
easing: "easeInOutSine",
}, 0);
}
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("prev-mode-button").addEventListener("click", () => {
setModeIndex(getModeIndex() - 1);
});
document.getElementById("next-mode-button").addEventListener("click", () => {
setModeIndex(getModeIndex() + 1);
});
document.getElementById("answer-form").addEventListener("submit", event => {
checkAnswer();
event.preventDefault();
});
setModeIndex(getModeIndex()); // initialize mode from URL
});