-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
77 lines (75 loc) · 2.63 KB
/
main.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
const DOM = {
"problems": document.getElementById("problems"),
"submit": document.getElementById("calc"),
"inst": document.getElementById("instructions"),
"toggle": document.getElementById("showinst")
};
DOM.toggle.onclick = () => {
if (DOM.inst.style.display == "none") {
DOM.inst.style.display = "";
DOM.toggle.innerText = "Hide instructions";
} else {
DOM.inst.style.display = "none";
DOM.toggle.innerText = "Show instructions";
}
};
DOM.submit.onclick = () => {
DOM.submit.disabled = true;
let problems = DOM.problems.value.toLowerCase().trim().split(", "); // [ "5-61 eoo", "62", "67" ]
let totalProblems = 0;
let probStr = "";
let addProblem = (num) => {
++totalProblems;
if (probStr.length == 0) probStr += num; else probStr += ", " + num;
};
for (let i of problems) {
if (i.indexOf("-") == -1) addProblem(i); else {
let start = parseInt(i.split("-")[0]);
let end = parseInt(i.split("-")[1].split(" ")[0]);
if (isNaN(start) || isNaN(end)) {
window.alert("Unknown number: " + (isNaN(start) ? start : end));
window.location.reload();
return;
}
let operator = i.split(" ")[1] ?? "all";
switch (operator) {
case "eoo": {
let add = true;
for (let i = start; i <= end; ++i) {
if (i % 2 != 0) {
if (add) addProblem(i);
add = !add;
}
}
break;
}
case "odds":
case "(pick": {
for (let i = start; i <= end; ++i) {
if (i % 2 != 0) addProblem(i);
}
break;
}
case "evens": {
for (let i = start; i <= end; ++i) {
if (i % 2 == 0) addProblem(i);
}
break;
}
case "all": {
for (let i = start; i <= end; ++i) addProblem(i);
break;
}
default: {
window.alert("Unknown operator: " + operator);
window.location.reload();
return;
}
}
}
}
document.getElementById("card-body").innerHTML = `<b>Total: ${totalProblems}</b><br /><br />Problems: ${probStr}`;
};
window.onload = () => {
DOM.submit.disabled = false;
};