-
Notifications
You must be signed in to change notification settings - Fork 1
/
learn.ts
106 lines (105 loc) · 2.99 KB
/
learn.ts
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
import { Card } from "./typedef.ts";
import { sets } from "./main.ts";
import { shuffleArray } from "https://deno.land/x/[email protected]/mod.ts";
import { Chrono } from "https://deno.land/x/[email protected]/mod.ts";
import { infoIfVerbose, writeSets } from "./functions.ts";
export function learn(
options: { verbose?: unknown; allCards?: true | undefined },
setName: string
) {
const rawSet: Card[] = sets[setName];
let set = rawSet;
if (rawSet.length == 0) {
console.error("You have nothing to learn. Add some cards first.");
Deno.exit();
}
for (let i = 6; i > 0; i--) {
const filtered = rawSet.filter((item) => item.phase == i);
if (filtered.length > 0) {
infoIfVerbose(
`Discarding cards of phase ${
filtered[0].phase + 1
} and replacing them with ${filtered.length} cards of a lower phase`
);
set = filtered;
}
}
const incorrect: Card[] = [];
const today = new Chrono(new Chrono().toISOString().split("T")[0]);
const fullSet = set;
for (const card of set) {
let nextDate: Chrono = new Chrono(new Chrono().toISOString().split("T")[0]);
switch (card.phase) {
case 1:
break;
case 2:
nextDate = new Chrono(nextDate.addDay(1));
break;
case 3:
nextDate = new Chrono(nextDate.addDay(2));
break;
case 4:
nextDate = new Chrono(nextDate.addDay(4));
break;
case 5:
nextDate = new Chrono(nextDate.addDay(8));
break;
case 6:
nextDate = new Chrono(nextDate.addDay(16));
break;
default:
break;
}
if (nextDate > today) {
infoIfVerbose(
`Discarding card ${card.source}, because next date ${nextDate} is after ${today}`
);
set.splice(set.indexOf(card), 1);
}
}
if (set.length == 0) {
console.error("You have nothing to learn today. Hooray! 🎉");
if (
prompt(
"Learn anyway (may impact learning effect and induce burnout)? (yes or no)",
"no"
) == "no" &&
!options.allCards
) {
Deno.exit();
} else {
set = fullSet;
}
}
set = shuffleArray(set).slice(0, 20);
console.info(`Learning vocabulary in phase ${set[0].phase}`);
for (const i in set) {
const card = set[i];
alert(card.source);
alert(card.translation);
let correct: string | null = null;
while (correct != "yes" && correct != "no") {
correct = prompt("Was your answer correct? (yes or no)", "yes");
}
if (correct == "no") {
set[i].phase -= 1;
incorrect.push(card);
} else {
set[i].phase += 1;
}
}
while (incorrect.length > 0) {
const card = incorrect.pop();
alert(card?.source);
console.info(card?.translation);
let correct: string | null = null;
while (correct != "yes" && correct != "no") {
correct = prompt("Was your answer correct? (yes or no)", "yes");
}
if (correct == "no") {
incorrect.push(card!);
}
}
sets[setName] = set
writeSets(sets)
}