-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
109 lines (104 loc) · 3.52 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
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
function moduleSubmit(event) {
const tableBody = document.querySelector("#modulesTable tbody");
const html =
`<tr>
<td>${event.target[0].value}</td>
<td class="text-end">${event.target[1].value}</td>
<td class="text-end">${event.target[2].value}</td>
<td class="text-end">${event.target[3].value}</td>
<td class="text-end"><button class="btn btn-outline-danger" onclick="removeModule(this)">X</button></td>
</tr>`;
tableBody.insertAdjacentHTML('beforeend', html);
updateClassification();
event.preventDefault();
}
function removeModule(removeBtn) {
removeBtn.closest('tr').remove();
updateClassification();
}
function updateClassification() {
const classElement = document.getElementById("classification")
const classification = calculateClass();
switch (classification) {
case 1:
classElement.textContent = "First Class";
break;
case 2:
classElement.textContent = "Upper Second Class (2:1)";
break;
case 3:
classElement.textContent = "Lower Second Class (2:2)";
break;
case 4:
classElement.textContent = "Third Class";
break;
default:
classElement.textContent = "Not enough credits entered"
break;
}
}
function calculateClass() {
let lvl2CredsNeeded = 120,
lvl3CredsNeeded = 120,
lvl2Modules = [],
lvl3Modules = [];
const submittedModules = document.querySelectorAll("#modulesTable tbody tr");
console.log(submittedModules);
for (const moduleData of submittedModules) {
const module = {
level: Number.parseInt(moduleData.children[1].textContent),
grade: Number.parseInt(moduleData.children[2].textContent),
credits: Number.parseInt(moduleData.children[3].textContent)
}
if (module.level === 2) {
lvl2CredsNeeded -= module.credits;
lvl2Modules.push(module);
} else {
lvl3CredsNeeded -= module.credits;
lvl3Modules.push(module);
}
}
if (lvl2CredsNeeded > 0 || lvl3CredsNeeded > 0) {
return -1;
}
lvl2Modules = getTop120Credits(lvl2Modules);
lvl3Modules = getTop120Credits(lvl3Modules);
let moduleScore = 0;
for (const module of lvl2Modules) {
moduleScore += module.grade * module.credits;
}
for (const module of lvl3Modules) {
moduleScore += (module.grade * module.credits) * 2;
}
if (moduleScore <= 630 || (moduleScore <= 690 && testBorderline(lvl3Modules, 1))) {
return 1;
} else if (moduleScore <= 900 || (moduleScore <= 960 && testBorderline(lvl3Modules, 2))) {
return 2;
} else if (moduleScore <= 1170 || (moduleScore <= 1230 && testBorderline(lvl3Modules, 3))) {
return 3;
} else {
return 4;
}
}
function getTop120Credits(modules) {
modules.sort((a, b) => (a.grade - b.grade));
let credits = 0;
const topModules = [];
while (credits < 120) {
const module = modules.shift();
topModules.push(module);
credits += module.credits;
}
return topModules;
}
function testBorderline(modules, minimumGrade) {
let creditsAtMinGrade = 0;
for (const module of modules) {
if (module.grade <= minimumGrade) {
creditsAtMinGrade += module.credits;
}
}
return creditsAtMinGrade >= 60;
}
const form = document.getElementById("moduleForm");
form.addEventListener("submit", moduleSubmit);