-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
276 lines (217 loc) · 8.29 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
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
const hiragana = {
a: { a: [["a", "あ"]], i: [["i", "い"]], u: [["u", "う"]], e: [["e", "え"]], o: [["o", "お"]] },
ka: { ka: [["ka", "か"], ["ga", "が"]], ki: [["ki", "き"], ["gi", "ぎ"]], ku: [["ku", "く"], ["gu", "ぐ"]], ke: [["ke", "け"], ["ge", "げ"]], ko: [["ko", "こ"], ["go", "ご"]] },
sa: { sa: [["sa", "さ"], ["za", "ざ"]], shi: [["shi", "し"], ["ji", "じ"]], su: [["su", "す"], ["zu", "ず"]], se: [["se", "せ"], ["ze", "ぜ"]], so: [["so", "そ"], ["zo", "ぞ"]] },
ta: { ta: [["ta", "た"], ["da", "だ"]], chi: [["chi", "ち"], ["ji", "ぢ"]], tsu: [["tsu", "つ"], ["zu", "づ"]], te: [["te", "て"], ["de", "で"]], to: [["to", "と"], ["do", "ど"]] },
na: { na: [["na", "な"]], ni: [["ni", "に"]], nu: [["nu", "ぬ"]], ne: [["ne", "ね"]], no: [["no", "の"]] },
ha: { ha: [["ha", "は"], ["ba", "ば"], ["pa", "ぱ"]], hi: [["hi", "ひ"], ["bi", "び"], ["pi", "ぴ"]], fu: [["fu", "ふ"], ["bu", "ぶ"], ["pu", "ぷ"]], he: [["he", "へ"], ["be", "べ"], ["pe", "ぺ"]], ho: [["ho", "ほ"], ["bo", "ぼ"], ["po", "ぽ"]] },
ma: { ma: [["ma", "ま"]], mi: [["mi", "み"]], mu: [["mu", "む"]], me: [["me", "め"]], mo: [["mo", "も"]] },
ya: { ya: [["ya", "や"]], yu: [["yu", "ゆ"]], yo: [["yo", "よ"]] },
ra: { ra: [["ra", "ら"]], ri: [["ri", "り"]], ru: [["ru", "る"]], re: [["re", "れ"]], ro: [["ro", "ろ"]] },
wa: { wa: [["wa", "わ"]], wo: [["wo", "を"]] },
n: { n: [["n", "ん"]] },
};
const katakana = {
A: { A: [["A", "ア"]], I: [["I", "イ"]], U: [["U", "ウ"]], E: [["E", "エ"]], O: [["O", "オ"]] },
KA: { KA: [["KA", "カ"], ["GA", "ガ"]], KI: [["KI", "キ"], ["GI", "ギ"]], KU: [["KU", "ク"], ["GU", "グ"]], KE: [["KE", "ケ"], ["GE", "ゲ"]], KO: [["KO", "コ"], ["GO", "ゴ"]] },
SA: { SA: [["SA", "サ"], ["ZA", "ザ"],], SHI: [["SHI", "シ"], ["JI", "ジ"],], SU: [["SU", "ス"], ["ZU", "ズ"],], SE: [["SE", "セ"], ["ZE", "ゼ"],], SO: [["SO", "ソ"], ["ZO", "ゾ"],] },
TA: { TA: [["TA", "タ"], ["DA", "ダ"]], CHI: [["CHI", "チ"], ["JI", "ヂ"]], TSU: [["TSU", "ツ"], ["JU", "ヅ"]], TE: [["TE", "テ"], ["DE", "デ"]], TO: [["TO", "ト"], ["DO", "ド"]] },
NA: { NA: [["NA", "ナ"]], NI: [["NI", "ニ"]], NU: [["NU", "ヌ"]], NE: [["NE", "ネ"]], NO: [["NO", "ノ"]] },
HA: { HA: [["HA", "ハ"], ["BA", "バ"], ["PA", "パ"]], HI: [["HI", "ヒ"], ["BI", "ビ"], ["PI", "ピ"]], FU: [["FU", "フ"], ["BU", "ブ"], ["PU", "プ"]], HE: [["HE", "ヘ"], ["BE", "ベ"], ["PE", "ペ"]], HO: [["HO", "ホ"], ["BO", "ボ"], ["PO", "ポ"]] },
MA: { MA: [["MA", "マ"]], MI: [["MI", "ミ"]], MU: [["MU", "ム"]], ME: [["ME", "メ"]], MO: [["MO", "モ"]] },
YA: { YA: [["YA", "ヤ"]], YU: [["YU", "ユ"]], YO: [["YO", "ヨ"]] },
RA: { RA: [["RA", "ラ"]], RI: [["RI", "リ"]], RU: [["RU", "ル"]], RE: [["RE", "レ"]], RO: [["RO", "ロ"]] },
WA: { WA: [["WA", "ワ"]], WO: [["WO", "ヲ"]] },
N: { N: [["N", "ン"]] },
};
let right = 0;
let wrong = 0;
let last;
let current;
let currentList = (() => {
const list = JSON.parse(localStorage.getItem("list"));
if (!list || typeof list != "object") return {};
return list;
})();
let useAccents = JSON.parse(localStorage.getItem("accents")) ?? false;
checkAllBoxes();
start();
function start() {
checkStart();
current = getRandomKana();
console.log(current);
document.getElementsByClassName("kana")[0].textContent = current[1];
document.getElementsByClassName("score")[0].textContent = `🟢 ${right} | ${wrong} 🔴`;
document.getElementById("input").value = "";
}
function onButtonClick(box) {
const { id } = box;
if (id == "Accents") {
useAccents = !useAccents;
setItem(2);
return;
}
if (currentList[id]) return removeFromList(id);
const kanaList = getKanaList(id);
addToList(id, kanaList[id]);
}
function onSubmit() {
const input = (() => {
const rawInput = document.getElementById("input").value;
if (!rawInput || typeof rawInput != "string") return null;
// Si c'est un hiragana, on met son input en minuscule, sinon en majuscule
if (current[0] == current[0].toLowerCase()) return rawInput.toLowerCase();
return rawInput.toUpperCase();
})();
if (!input) return;
if (input == current[0]) {
right++;
return start();
}
const goodKanas = isValidInput(input);
if (!goodKanas.length) return alert("Le son donné n'est pas valide");
alert(`La réponse était: ${current[0]}\nLe son ${input} correspond à ${goodKanas.join(" | ")}`);
wrong++;
start();
}
function isValidInput(input) {
const defaultKanaList = getKanaList(input);
const goodKanas = [];
for (const [, listOfSounds] of Object.entries(defaultKanaList)) {
for (const [, kanaList] of Object.entries(listOfSounds)) {
for (const kana of kanaList) {
if (kana[0] == input) goodKanas.push(kana[1]);
}
}
}
return goodKanas;
}
function getRandomKana() {
const list = [];
for (const [, listOfSounds] of Object.entries(currentList)) {
for (const [, kanaList] of Object.entries(listOfSounds)) {
// Ne pas mettre les accents s'ils ne sont pas cochés
if (!useAccents) {
list.push(kanaList[0]);
continue;
}
for (const kana of kanaList) {
list.push(kana);
}
}
}
const num = getRandomNumber(list.length);
const data = list[num]; // ["a", "あ"]
return data;
}
function getRandomNumber(len) {
let num = Math.floor(Math.random() * len);
// Si seulement ん est coché num sera toujours last
if (num == last && len > 1) return getRandomNumber(len);
last = num;
return num;
}
function checkStart() {
const boxes = document.getElementsByClassName("box");
const list = {};
for (let i = 0; i < boxes.length; i++) {
const element = boxes[i];
const { id, checked } = element;
if (id == "Accents") {
useAccents = checked;
continue;
}
if (!checked) {
if (currentList[id]) removeFromList(id);
continue;
}
list[id] = getKanaList(id)[id];
if (!currentList[id]) addToList(id, list[id]);
}
if (!Object.keys(list).length) {
boxes[0].checked = true;
return addToList("a", hiragana.a);
}
if (localStorage.getItem("list") != JSON.stringify(currentList)) setItem();
}
function addToList(id, kana) {
currentList[id] = kana;
setItem();
}
function removeFromList(id) {
delete currentList[id];
setItem();
}
/**
* 0 - les deux
* 1 - list
* 2 - accents
*/
function setItem(type = 0) {
try {
if (type == 0 || type == 1) localStorage.setItem("list", JSON.stringify(currentList));
if (type == 0 || type == 2) localStorage.setItem("accents", useAccents);
} catch (error) {
localStorage.clear();
alert("Impossible de sauvegarder");
}
}
/**
* 0 - normal check
* 1 - uncheck all
* 2 - check all
*/
function checkAllBoxes(action = 0) {
const boxes = document.getElementsByClassName("box");
for (const box of boxes) {
if (action == 1) {
box.checked = false;
continue;
}
if (action == 2) {
box.checked = true;
continue;
}
if (box.id == "Accents") {
box.checked = useAccents;
continue;
}
box.checked = !!currentList[box.id];
}
}
function triggerAction() {
const action = prompt(`Quelle action voulez-vous faire ?
0 - Reset le score
1 - Décocher tout les kana
2 - Cocher tout les kana
3 - Reset complet
`, "3");
if (action == "0") {
right = 0;
wrong = 0;
return start();
}
if (action == "1") {
checkAllBoxes(1);
return start();
}
if (action == "2") {
checkAllBoxes(2);
return start();
}
if (action == "3") {
return reset();
}
}
function reset() {
const doReset = confirm("Êtes-vous sûr de vouloir réinitialiser ?");
if (!doReset) return;
localStorage.clear();
right = 0;
wrong = 0;
checkAllBoxes(1);
start();
}
function getKanaList(sound) {
return sound == sound.toLowerCase() ? hiragana : katakana;
}