-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflashcard.js
More file actions
56 lines (45 loc) · 1.44 KB
/
flashcard.js
File metadata and controls
56 lines (45 loc) · 1.44 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
const infoText = document.querySelector("#info-text");
function next() {
infoText.textContent = "";
const candiates = [...window.ITEMS];
const nextRandomItem = () => {
const idx = Math.floor(Math.random() * candiates.length);
const result = candiates[idx];
candiates.splice(idx, 1);
return result;
};
const item = nextRandomItem();
const mangle = Math.random() > 0.5;
const question = `What is the ${mangle ? "" : "de"}mangled form of this?`;
const children = card.querySelectorAll("#card > *");
children[0].textContent = question;
children[1].textContent = mangle ? item.means : item.mangle;
const options = [
nextRandomItem(item),
nextRandomItem(item),
nextRandomItem(item),
nextRandomItem(item),
];
const correctIdx = Math.floor(Math.random() * options.length);
options[correctIdx] = item;
children[2].replaceChildren(
...options.map((opt, i) => {
const isCorrect = i === correctIdx;
const text = document.createElement("li");
text.classList.add("flashcard-option");
text.textContent = mangle ? opt.mangle : opt.means;
text.tabIndex = "0";
text.addEventListener("click", () => {
if (isCorrect) {
infoText.classList.remove("flashcard-error");
next();
} else {
infoText.classList.add("flashcard-error");
infoText.textContent = "Incorrect.";
}
});
return text;
})
);
}
next();