-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
87 lines (78 loc) · 2.74 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
const add = document.querySelector("#add");
const courseCode = document.querySelector("#course-code");
const unitLoad = document.querySelector("#unit-load");
const grade = document.querySelector("#grade");
const tbody = document.querySelector("#tbody");
const tfoot = document.querySelector("#tfoot");
const table = document.querySelector("#table");
const calcGp = document.querySelector("#calc-gp");
const clear = document.querySelector("#clear");
// starting of arrays.............
let gpArry = [];
add.addEventListener("click", () => {
if (
courseCode.value === "" ||
unitLoad.value <= 0 ||
grade.selectedIndex === 0
) {
alert ("Wrong input,check and try again ...");
} else {
const tr = document.createElement("tr");
const tdCourseCode = document.createElement("td");
tdCourseCode.innerHTML = courseCode.value;
const tdUnitLoad = document.createElement("td");
tdUnitLoad.innerHTML = unitLoad.value;
const tdGrade = document.createElement("td");
tdGrade.innerHTML = grade.options[grade.selectedIndex].text;
tr.appendChild(tdCourseCode);
tr.appendChild(tdUnitLoad);
tr.appendChild(tdGrade);
tbody.appendChild(tr);
table.classList.remove("display-none");
calcGp.classList.remove("display-none");
clear.classList.remove("display-none");
gpArry.push({
unitLoad: unitLoad.value,
grade: grade.options[grade.selectedIndex].value,
});
console.log(gpArry);
courseCode.value = "";
unitLoad.value = "";
grade.selectedIndex = "0";
}
});
calcGp.addEventListener("click", () => {
let unitLoads = 0,
productOfUnitLoadsAndGrades = 0,
sumOfProductOfUnitLoadsAndGrades = 0;
gpArry.forEach((result) => {
unitLoads += parseInt(result.unitLoad);
productOfUnitLoadsAndGrades =
parseInt(result.unitLoad) * parseInt(result.grade);
sumOfProductOfUnitLoadsAndGrades += productOfUnitLoadsAndGrades;
});
const tr = document.createElement("tr");
tdTotalUnitLoad = document.createElement("td");
tdTotalUnitLoad.innerHTML = `Your Total Unit Load is : ${unitLoads}`;
tdGpa = document.createElement("td");
tdGpa.setAttribute("colspan", "2");
tdGpa.innerHTML = `Your CGPA is : ${(
sumOfProductOfUnitLoadsAndGrades / unitLoads
).toFixed(2)} `;
tr.appendChild(tdTotalUnitLoad);
tr.appendChild(tdGpa);
if (tfoot.querySelector("tr") !== null) {
tfoot.querySelector("tr").remove();
}
tfoot.appendChild(tr);
});
clear.addEventListener("click", () => {
gpArry = [];
tbody.querySelectorAll("*").forEach((child) => child.remove());
if (tfoot.querySelector("tr") !== null) {
tfoot.querySelector("tr").remove();
}
table.classList.add("display-none");
calcGp.classList.add("display-none");
clear.classList.add("display-none");
});