-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (183 loc) · 6.51 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Select relevant DOM elements
const btn = document.getElementById("submit-btn");
const addCourseBtn = document.getElementById("add-btn");
const courseForm = document.getElementById("course-form");
const gp = document.getElementById("gp");
const overallCgpaBox = document.getElementById("overall-cgpa");
const currentCgpaInput = document.getElementById("current-cgpa");
const completedCreditsInput = document.getElementById("completed-credits");
const statusBox = document.getElementById("status-box"); // Status box for GPA messages
// Event listener for the submit button
btn.addEventListener("click", () => {
let data = [];
// Gather form data
for (let i = 0; i < courseForm.children.length; i++) {
let col = [];
const inputs = courseForm.children[i].querySelectorAll("input, select");
for (let j = 1; j < inputs.length; j++) {
col.push(inputs[j].value);
}
data.push(col);
}
calculateGP(data); // Call to calculate GPA and update gauge
});
// Event listener for adding a new course input row
addCourseBtn.addEventListener("click", () => {
// Create form input row
const div = document.createElement("div");
div.className = "col";
// Create course code input box
const courseCodeBox = document.createElement("input");
courseCodeBox.type = "text";
courseCodeBox.placeholder = "Course Code";
// Create course unit input box
const courseUnitBox = document.createElement("input");
courseUnitBox.type = "number";
courseUnitBox.placeholder = "Credits";
// Create grade select input box
const select = document.createElement("select");
select.innerHTML = `
<option value="A+">A+</option>
<option value="A">A</option>
<option value="A-">A-</option>
<option value="B+">B+</option>
<option value="B">B</option>
<option value="B-">B-</option>
<option value="C+">C+</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="F">F</option>
`;
// Create remove course button
const removeBtn = document.createElement("button");
removeBtn.className = "remove-course";
removeBtn.innerHTML = `<i class="fa fa-times"></i>`;
removeBtn.addEventListener("click", (e) => {
const self = e.target.closest('.col');
removeChild(self);
});
// Add input boxes to row
div.appendChild(courseCodeBox);
div.appendChild(courseUnitBox);
div.appendChild(select);
div.appendChild(removeBtn);
// Add row div to form
courseForm.appendChild(div);
});
// Function to calculate GPA and CGPA
function calculateGP(data) {
const gradeMapping = {
"A+": 4.00,
"A": 3.75,
"A-": 3.50,
"B+": 3.25,
"B": 3.00,
"B-": 2.75,
"C+": 2.50,
"C": 2.25,
"D": 2.00,
"F": 0
};
let totalUnits = 0;
let cummPoints = 0;
for (let value of data) {
let [unit, grade] = value;
unit = Number(unit);
totalUnits += unit;
cummPoints += gradeMapping[grade] * unit;
}
let newGPA = cummPoints / totalUnits;
gp.textContent = `GPA: ${newGPA.toFixed(2)}`;
// Determine and display status based on GPA value
let status = "";
if (newGPA >= 4) {
status = "Excellent";
} else if (newGPA >= 3.75) {
status = "Very Good";
} else if (newGPA >= 3.50) {
status = "Good";
}else if (newGPA >= 3.00) {
status = "Averange";
} else if (newGPA >= 2.50) {
status = "Poor";
}else if (newGPA >= 2.00) {
status = "Bad";
} else {
status = "Very Bad";
}
statusBox.textContent = `${status}`; // Display the status message
// Calculate overall CGPA
let currentCgpa = parseFloat(currentCgpaInput.value);
let completedCredits = parseFloat(completedCreditsInput.value);
let overallCgpa = ((currentCgpa * completedCredits) + (newGPA * totalUnits)) / (completedCredits + totalUnits);
overallCgpaBox.textContent = `CGPA: ${overallCgpa.toFixed(2)}`;
// Map GPA to a custom gauge percentage and update the gauge
let gpaPercentage = mapGpaToGauge(newGPA); // Custom mapping function
const gaugeMeter = document.querySelector(".gauge-box");
GaugeChart_SetPercent(gaugeMeter, gpaPercentage); // Pass the mapped gauge percentage
}
// Custom function to map GPA to the gauge meter
function mapGpaToGauge(gpa) {
if (gpa >= 4) {
return 100; // Excellent
} else if (gpa >= 3.90) {
return 95; // Very Good
} else if (gpa >= 3.80) {
return 88; // Very Good
} else if (gpa >= 3.75) {
return 80; // Very Good
} else if (gpa >= 3.50) {
return 75; // Good
} else if (gpa >= 3.30) {
return 65; // Good
} else if (gpa >= 3.00) {
return 55; // Good
} else if (gpa >= 2.75) {
return 50; // Average
} else if (gpa >= 2.50) {
return 40; // Average
} else if (gpa >= 2.30) {
return 30; // Average
} else if (gpa >= 2.00) {
return 25; // Poor
} else {
return 0; // Very Bad
}
}
// Update the gauge pointer based on GPA percentage
function GaugeChart_SetPercent(el, _perc) {
el.dataset.percent = _perc; // Update the gauge meter with new percent value
GaugeChart_Animate(el); // Reanimate the pointer based on new percent value
}
// Function to animate the gauge pointer
function GaugeChart_Animate(el) {
var pointer = el.querySelector(".pointer");
if (!pointer) return;
var percent_deg = 1.8; // Degree of movement per percent
var _perc = parseInt(el.dataset.percent);
var percent_deg_style = _perc * percent_deg - 90;
if (percent_deg_style < -90) percent_deg_style = -90;
if (percent_deg_style > 90) percent_deg_style = 90;
pointer.style.transform = `rotateZ(${percent_deg_style}deg)`;
// Change color or flame gauge effect based on high GPA
if (_perc >= 87.5) {
el.querySelector(".flame-gauge").classList.add("active");
} else {
el.querySelector(".flame-gauge").classList.remove("active");
}
}
// Function to remove a course row
function removeChild(self) {
if (courseForm.children.length > 1) {
courseForm.removeChild(self);
}
}
// Ensure the initial course remove buttons are functional
document.querySelectorAll(".remove-course").forEach((btn) => {
btn.addEventListener("click", (e) => {
const self = e.target.closest('.col');
removeChild(self);
});
});
// Initialize gauge behavior
GaugeChart_BehaviorInit();