forked from FarzanFrost/GPA-UCSC-Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_gpa.js
237 lines (209 loc) · 9.15 KB
/
calculate_gpa.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const get_gpv = (result) => {
if (result === 'A' || result ==='A+') {
return 4.00;
} else if (result === 'A-') {
return 3.70;
}else if (result === 'B+'){
return 3.30;
}else if (result === 'B'){
return 3.00;
}else if (result === 'B-'){
return 2.70;
}else if (result === 'C+' || result === 'A+ : C+' || result === 'A : C+' || result === 'A- : C+' || result === 'B+ : C+' || result === 'B : C+' || result === 'B- : C+' || result === 'C+ : C+'){
return 2.30;
}else if (result === 'C'){
return 2.00;
}else if(result === 'C-'){
return 1.70;
}else if(result === 'D+'){
return 1.30;
}else if (result === 'D'){
return 1.00;
}else if (result === 'E' || result === 'F'){
return 0;
}else{
return -1;
}
}
const calculate_gpa = () => {
result_records = Array.from(document.getElementsByTagName("tr"))
if (result_records.length > 0){
let selected_credits = 0;
let total_credits = 0;
let prod_credit_results = 0;
result_records.forEach((row) => {
credit = parseInt(row.innerText.split("\t")[3])
result = get_gpv(row.innerText.split("\t")[4])
included_in_gpa = row.innerText.split("\t")[5]
is_included_in_gpa = true;
if(!isNaN(included_in_gpa)){
checkbox = row.querySelectorAll('input')
is_included_in_gpa = checkbox[0].checked
}
if (credit === 0 || isNaN(credit) || result === -1) {
return;
}
total_credits += credit;
if (!is_included_in_gpa) return;
selected_credits += credit
prod_credit_results += credit * result
});
console.log("CGPA is : " + prod_credit_results);
GPA = prod_credit_results/selected_credits
gpa_class = get_class(GPA)
GPA = GPA.toFixed(4)
gpa_element = document.getElementById('gpa');
gpa_class_element = document.getElementById('gpa_class');
credits_element = document.getElementById('applicable_credits');
total_credits_element = document.getElementById('total_credits');
if(gpa_element){
gpa_element.textContent = `GPA : ${GPA}`;
if(gpa_class !== undefined) gpa_class_element.textContent = `Class : ${gpa_class}`
else gpa_class_element.textContent = ''
credits_element.textContent = `No of Selected Credits : ${selected_credits}`;
if(total_credits == selected_credits) credits_element.style.display = 'none'
else credits_element.style.display = 'block';
total_credits_element.textContent = `No of Total Credits : ${total_credits}`;
}else{
alert(`GPA : ${GPA}\nClass : ${gpa_class}`);
}
}
}
const get_class = (GPA) => {
if (GPA >= 3.7){
return 'First Class'
}else if(GPA >= 3.3){
return 'Second Class (Upper Division)'
}else if (GPA >= 3){
return 'Second Class (Lower Division)'
}else{
return
}
}
const get_tables = () => {
return Array.from(document.getElementsByTagName("table"))
}
const modify_page = (tables) => {
GPA = null
gpa_class = null
selected_credits = null
total_credits = null
var creditsh5Element = document.createElement('h6');
var creditsspanElement = document.createElement('span');
creditsspanElement.id = 'applicable_credits'
creditsspanElement.textContent = `Selected Credits : ${selected_credits}`;
creditsh5Element.appendChild(creditsspanElement)
var total_credits_h5Element = document.createElement('h6');
var total_credits_spanElement = document.createElement('span');
total_credits_spanElement.id = 'total_credits'
total_credits_spanElement.textContent = `Total Credits : ${total_credits}`;
total_credits_h5Element.appendChild(total_credits_spanElement)
var gpah5Element = document.createElement('h5');
var gpastrongElement = document.createElement('strong');
gpastrongElement.id = 'gpa'
gpastrongElement.textContent = `GPA : ${GPA}`;
gpah5Element.appendChild(gpastrongElement)
var gpa_class_h5Element = document.createElement('h5');
var gpa_class_strongElement = document.createElement('strong');
gpa_class_strongElement.id = 'gpa_class'
gpa_class_strongElement.textContent = `Class : ${gpa_class}`;
gpa_class_h5Element.appendChild(gpa_class_strongElement)
var primaryTag = document.getElementById('primary');
let gpaInsertLocation = 4
if (primaryTag) {
if (primaryTag.children.length >= gpaInsertLocation) {
primaryTag.insertBefore(gpah5Element, primaryTag.children[gpaInsertLocation]);
primaryTag.insertBefore(gpa_class_h5Element, primaryTag.children[gpaInsertLocation+1]);
primaryTag.insertBefore(creditsh5Element, primaryTag.children[gpaInsertLocation+2]);
primaryTag.insertBefore(total_credits_h5Element, primaryTag.children[gpaInsertLocation+3]);
gpah5Element.className = primaryTag.children[gpaInsertLocation].className;
gpa_class_h5Element.className = primaryTag.children[gpaInsertLocation].className;
creditsh5Element.className = primaryTag.children[gpaInsertLocation].className;
total_credits_h5Element.className = primaryTag.children[gpaInsertLocation].className;
} else {
console.error("The div doesn't have enough children.");
}
} else {
alert(`GPA : ${GPA}`);
}
repeat_subject_list = []
for(i = 0; i < tables.length; i++){
rows = tables[i].querySelectorAll('tr')
rows.forEach((row) => {
credit = row.innerText.split("\t")[3]
if (credit.toLowerCase() === 'credits'){
th = row.querySelectorAll('th')[0]
var checkboxCell = document.createElement('th');
checkboxCell.className = th.className
checkboxCell.textContent = 'Included in GPA'
row.appendChild(checkboxCell);
}else{
var checkboxDiv = document.createElement('div')
checkboxDiv.className = "form-check form-switch"
td = row.querySelectorAll('td')[0]
var checkboxCell = document.createElement('td');
checkboxCell.className = td.className
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.checked = true;
checkbox.addEventListener('change', function () {
calculate_gpa()
});
checkbox.className = "form-check-input bg-danger border-danger"
checkboxDiv.appendChild(checkbox)
checkboxCell.appendChild(checkboxDiv);
row.appendChild(checkboxCell);
}
});
}
}
const handle_repeat_subjects = (tables) => {
for(i = 0; i < tables.length; i++){
rows = tables[i].querySelectorAll('tr')
for(j = 0; j < rows.length; j++){
row_j = rows[j]
row_j_innerText = row_j.innerText.split("\t")
subject_name_j = row_j_innerText[0].toLowerCase().trim()
subject_year_j = parseInt(row_j_innerText[1].toLowerCase().trim().slice(1, -1));
result_j = row_j_innerText[4]
if(row_j === 'subject') continue
for(k = j + 1; k < rows.length; k++){
row_k = rows[k]
row_k_innerText = row_k.innerText.split("\t")
subject_name_k = row_k_innerText[0].toLowerCase().trim()
if (subject_name_j === subject_name_k) {
subject_year_k = parseInt(row_k_innerText[1].toLowerCase().trim().slice(1, -1));
result_k = row_k_innerText[4]
if (subject_year_j < subject_year_k && get_gpv(result_j) <= get_gpv(result_k) ) {
included_in_gpa = row_j_innerText[5]
if(!isNaN(included_in_gpa)){
checkbox_j = row_j.querySelectorAll('input')
checkbox_j[0].checked = false
checkbox_j[0].disabled = true
}
if (get_gpv('C+') <= get_gpv(result_k)){
row_k.querySelectorAll('td')[4].textContent = row_k.querySelectorAll('td')[4].textContent + ' : C+'
}
}
else {
included_in_gpa = row_k_innerText[5]
if(!isNaN(included_in_gpa)){
checkbox_k = row_k.querySelectorAll('input')
checkbox_k[0].checked = false
checkbox_k[0].disabled = true
}
}
}
}
}
}
}
const check_if_correct_page = () =>{
tables = get_tables()
if (tables.length > 0){
modify_page(tables)
handle_repeat_subjects(tables)
calculate_gpa()
}
}
check_if_correct_page()