-
Notifications
You must be signed in to change notification settings - Fork 1
/
numbers.js
107 lines (89 loc) · 3.24 KB
/
numbers.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
function paramVal(name, def) {
return url.searchParams.has(name) ? url.searchParams.get(name) : def;
}
var url = new URL(location.href);
var english = paramVal("english", "latin");
function checkEnglish(correct, guess) {
var answers = correct.toLowerCase().replace(/ ?\([^\)]*\) ?/g, "").replace(/;/g, ",").split(",");
var minguess = guess.toLowerCase().replace(/^(a|an|the|to) /, "").replace(/ ?\([^\)]*\) ?/g, "").replace(/!/g, "");
for (i = 0; i < answers.length; i++) {
var a = answers[i].replace(/!/g, "").replace(/^ */, "").replace(/ *$/, "").replace(/^(a|an|the|to) /, "");
if (a == minguess) {
return true;
}
}
return false;
}
function checkLatin(correct, guess) {
var answers = correct.replace(/ ?\([^\)]*\) ?/g, "").replace(/;/g, ",").split(",");
if (answers[0] == "-") {
return (guess.toLowerCase() == answers[1].toLowerCase());
} else {
return (guess.toLowerCase() == answers[0].toLowerCase());
}
}
function checkAnswer(num) {
var check = document.getElementById("check" + num);
var result = document.getElementById("result" + num);
var correct = false;
if (english == 'latin') {
correct = checkLatin(check.name, check.value);
} else {
correct = checkEnglish(check.name, check.value);
}
if (correct) {
result.innerHTML = "<font color=\"green\">Correct!</font> " + check.name;
} else {
result.innerHTML = "<font color=\"red\">Incorrect!</font> " + check.name;
}
return false;
}
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function buildTable() {
var numtable = document.getElementById("numtable");
var list = vocab.filter((word) => word.chapter == 15 &&
(word.type == 'adjective, cardinal' || word.type == 'adjective, ordinal'));
if (english != 'ordered')
shuffle(list);
list.forEach((word, idx) => {
var answer;
var tr = document.createElement("tr");
var td = document.createElement("td");2
td.valign = "top";
if (english == 'latin') {
td.innerText = word.latin;
answer = word.english;
} else {
td.innerText = word.english;
answer = word.latin;
}
tr.appendChild(td);
td = document.createElement("td");
tr.appendChild(td);
td.valign = "bottom";
var form = document.createElement("form");
form.autocomplete = false;
form.autocorrect = false;
form.spellcheck = false;
td.appendChild(form);
form.onsubmit = (() => { return checkAnswer(idx); });
var input = document.createElement("input");
input.name = answer;
input.id = `check${idx}`;
input.type = "text";
input.autocomplete = false;
input.autocorrect = false;
input.spellcheck = false;
form.appendChild(input);
var label = document.createElement("label");
label.id = `result${idx}`;
form.appendChild(label);
numtable.appendChild(tr);
});
}
addEventListener("load", () => GetVocab(buildTable));