-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpaCalc.html
145 lines (136 loc) · 4.1 KB
/
gpaCalc.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="./assets/bootstrap.min.css" rel="stylesheet">
<title>GPA Calculator</title>
</head>
<body>
<div class="header">
<h1 id="title">GPA Table</h1>
</div>
<div class="main-table">
<table class="table table-dark table-borderless">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Courses</th>
<th scope="col">Grade</th>
<th scope="col">Credits</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Calculus</td>
<td class="grade">A+</td>
<td class="credit">4</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Physics</td>
<td class="grade">A</td>
<td class="credit">3</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Electronics</td>
<td class="grade">B+</td>
<td class="credit">3</td>
</tr>
<tr>
<th scope="row">4</th>
<td>Algorithm Design</td>
<td class="grade">A-</td>
<td class="credit">4</td>
</tr>
<tr>
<th scope="row">5</th>
<td>Probability</td>
<td class="grade">B-</td>
<td class="credit">2</td>
</tr>
<tr>
<th scope="row">6</th>
<td>Operating System</td>
<td class="grade">C+</td>
<td class="credit">2</td>
</tr>
<tr>
<th></th>
<td>Total</td>
<td id="grade-total"></td>
<td id="credit-total"></td>
</tr>
<tr>
<th></th>
<td>GPA</td>
<td></td>
<td id="gpa" style="border: 2px solid;"></td>
</tr>
</tbody>
</table>
</div>
</body>
<script>
calculate()
function calculate(){
var scale = {"A+": 4.0, "A": 4.0, "A-": 3.75, "B+": 3.5, "B": 3.0, "B-": 2.75, "C+": 2.5, "C": 2, "C-": 1.75, "D": 1.5, "F": 0};
//Output variables
var totalCrdtDisplay = document.getElementById("credit-total");
var totalGrdDisplay = document.getElementById("grade-total");
var gpaDisplay = document.getElementById("gpa");
//Input variables
var grd = document.getElementsByClassName("grade");
var crdt = document.getElementsByClassName("credit");
var inputLength = grd.length;
var grdList = [inputLength], crdtList = [inputLength], scaledList = [inputLength];
var totalCrdt = 0, totalScaled = 0, avgGrd;
let i;
for(i=0; i<inputLength; i++){
grdList[i] = grd[i].textContent;
scaledList[i] = scale[grdList[i]];
console.log(grdList[i]+" : "+scale[grdList[i]]);
}
for(i=0; i<inputLength; i++){
crdtList[i] = parseInt(crdt[i].textContent);
totalCrdt += crdtList[i];
}
for(i=0; i<inputLength; i++){
totalScaled += scaledList[i] * crdtList[i];
}
let gpa = (totalScaled / totalCrdt).toFixed(2);
//Using gpa to calculate estimated average grade
if(gpa >= 3.90){
avgGrd = "A+";
}else if(gpa >= 3.80 && gpa < 3.90){
avgGrd = "A";
}else if(gpa >= 3.70 && gpa < 3.80){
avgGrd = "A-";
}else if(gpa >= 3.40 && gpa < 3.70){
avgGrd = "B+";
}else if(gpa >= 2.90 && gpa < 3.40){
avgGrd = "B";
}else if(gpa >= 2.70 && gpa < 2.90){
avgGrd = "B-";
}else if(gpa >= 2.30 && gpa < 2.70){
avgGrd = "C+";
}else if(gpa >= 1.80 && gpa < 2.30){
avgGrd = "C";
}else if(gpa >= 1.70 && gpa < 1.80){
avgGrd = "C-";
}else if(gpa >= 1.50 && gpa < 1.70){
avgGrd = "D";
}else avgGrd = "F";
//DOMing the results
totalCrdtDisplay.innerText = totalCrdt;
totalGrdDisplay.innerText = avgGrd;
gpaDisplay.innerText = gpa;
//Styling the results
totalCrdtDisplay.style.color = totalGrdDisplay.style.color = gpaDisplay.style.color = 'aqua';
gpaDisplay.style.fontWeight = 600;
}
</script>
</html>