-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
325 lines (264 loc) · 10.5 KB
/
script.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Initial variables
var rowCount = 1;
// function initAccordion() {
// var acc = document.getElementsByClassName("accordion");
// for (var i = 0; i < acc.length; i++) {
// acc[i].addEventListener("click", function () {
// this.classList.toggle("active");
// var panel = this.nextElementSibling;
// if (panel.style.maxHeight) {
// panel.style.maxHeight = null;
// } else {
// panel.style.maxHeight = panel.scrollHeight + "px";
// }
// });
// }
// }
function addRow(holderName, holderGrade, holderWeight) {
var container = document.getElementById("container");
// Create a unique form
rowCount++;
var form = document.createElement("form");
form.className = "row";
form.setAttribute("id", "row" + rowCount);
// Divs
var nameDiv = document.createElement("div");
nameDiv.className = "col name";
var gradeDiv = document.createElement("div");
gradeDiv.className = "col num";
var weightDiv = document.createElement("div");
weightDiv.className = "col num";
// Create the input elements
var nameInput = document.createElement("input");
nameInput.type = "text";
nameInput.className = "selectable";
nameInput.id = "name";
if (holderName != undefined) { // if there is a value
nameInput.placeholder = holderName;
}
nameDiv.appendChild(nameInput);
form.appendChild(nameDiv);
var gradeInput = document.createElement("input");
gradeInput.type = "number";
gradeInput.className = "selectable";
gradeInput.id = "grade";
if (holderGrade != undefined) { // if there is a value
gradeInput.placeholder = holderGrade;
}
gradeDiv.appendChild(gradeInput);
form.appendChild(gradeDiv);
var weightInput = document.createElement("input");
weightInput.type = "number";
weightInput.className = "selectable";
weightInput.id = "weight";
if (holderWeight != undefined) { // if there is a value
weightInput.placeholder = holderWeight;
}
weightDiv.appendChild(weightInput);
form.appendChild(weightDiv);
// form.appendChild();
// form.appendChild();
// form.appendChild();
// Append the new row to the container
container.appendChild(form);
}
function initRows() {
addRow("", "", "");
addRow("", "", "");
addRow("", "", "");
}
function resetFinalGrade() {
var currentGrade = document.getElementById("currentGrade");
currentGrade.value = "";
var targetGrade = document.getElementById("targetGrade");
targetGrade.value = "";
var finalExamWeight = document.getElementById("finalExamWeight");
finalExamWeight.value = "";
}
function resetRows() {
var container = document.getElementById("container");
var firstRow = document.getElementById("row1");
firstRow.reset();
for (var i = rowCount; i > 1; i--) {
var row = document.getElementById(`row${i}`);
container.removeChild(row);
rowCount--;
}
initRows();
}
function findLetterGrade(average) {
if (average >= 95)
return "A+";
else if (average >= 87 && average <= 94)
return "A";
else if (average >= 80 && average <= 86)
return "A-";
else if (average >= 77 && average <= 79)
return "B+";
else if (average >= 72 && average <= 76)
return "B";
else if (average >= 70 && average <= 71)
return "B-";
else if (average >= 67 && average <= 69)
return "C+";
else if (average >= 63 && average <= 66)
return "C";
else if (average >= 60 && average <= 62)
return "C-";
else if (average >= 57 && average <= 59)
return "D+";
else if (average >= 54 && average <= 56)
return "D";
else if (average >= 51 && average <= 53)
return "D-";
else
return "F";
}
function writeOutput(average, weight) {
// var letterGrade = findLetterGrade(average);
// Retrieve the input elements
var outputGradeInput = document.getElementById("outputGrade");
outputGradeInput.value = average;
var outputWeightInput = document.getElementById("outputWeight");
outputWeightInput.value = weight;
var optionalOutput = document.getElementById("optionalGrade");
optionalOutput.innerHTML = "";
}
function writeOptionalOutput(average, weight, desiredGrade) {
var optionalOutput = document.getElementById("optionalGrade");
optionalOutput.innerHTML = "";
if (weight < 100) {
var rWeight = 100 - weight;
var requiredGrade = (desiredGrade - average) / (rWeight / 100);
requiredGrade = Math.round(requiredGrade * 100) / 100;
optionalOutput.innerHTML = `<p>You need to score an average of <span class=\"highlight_num\">${requiredGrade}%</span> for the remaining <span class=\"highlight_num\">${rWeight}%</span> to end with <span class=\"highlight_num\">${desiredGrade}%</span> in your class.</p>`;
}
}
function isNegative(a, b, c, d) {
if (a < 0 || b < 0 || c < 0 || d < 0) {
return true;
} else {
return false;
}
}
function calcFinalGrade() {
var finalGradeOutput = document.getElementById('finalGradeOutput');
finalGradeOutput.innerHTML = "";
var currentGradeInput = document.getElementById("currentGrade");
var currentGrade = Math.round(currentGradeInput.value * 100) / 100;
var targetGradeInput = document.getElementById("targetGrade");
var targetGrade = Math.round(targetGradeInput.value * 100) / 100;
var finalExamWeightInput = document.getElementById("finalExamWeight");
var finalExamWeight = Math.round(finalExamWeightInput.value * 100) / 100;
if (currentGrade < 0 || currentGrade > 100 || targetGrade < 0 || targetGrade > 100 || finalExamWeight < 0 || finalExamWeight >= 100) {
alert("Final Grade Calculator error. Check for the following:\n\n\u2022 Negative numbers (-)\n\u2022 Values over 100\n\u2022 Exponent symbols (\"e\" or \"E\")\n\u2022 Blank entries");
return;
}
var requiredGrade = (targetGrade - (currentGrade*((100-finalExamWeight)/100))) / ((finalExamWeight)/100);
requiredGrade = Math.round(requiredGrade * 100) / 100;
finalGradeOutput.innerHTML = `<p>You need to score an average of <span class=\"highlight_num\">${requiredGrade}%</span> to end with <span class=\"highlight_num\">${targetGrade}%</span> in your class.</p>`;
}
function calcCurrentGrade() {
var average = 0;
var totalWeight = 0;
for (var i = 1; i <= rowCount; i++) {
var form = document.getElementById("row" + i);
var gradeInput = form.querySelector("#grade");
var grade = parseFloat(gradeInput.value); // grade value
var weightInput = form.querySelector("#weight");
var weight = parseFloat(weightInput.value); // weight value
// If either inputs are left blank, then skip reading this row
if (isNaN(grade) || isNaN(weight))
continue;
// Rounding these values to 2 decimal places
grade = Math.round(grade * 100) / 100;
weight = Math.round(weight * 100) / 100;
average = average + (grade * (weight / 100));
average = Math.round(average * 100) / 100;
totalWeight = totalWeight + weight;
totalWeight = Math.round(totalWeight * 100) / 100;
if (totalWeight > 100 || isNegative(grade, weight, average, totalWeight)) {
alert("Current Grade Calculator error. Check for the following:\n\n\u2022 Negative numbers (-)\n\u2022 Values over 100\n\u2022 Exponent symbols (\"e\" or \"E\")");
return;
}
}
if (average == 0 && totalWeight == 0) return;
writeOutput(average, totalWeight);
var desiredGradeInput = document.getElementById("desiredGrade");
var desiredGrade = desiredGradeInput.value;
if (desiredGrade.trim() != "") {
writeOptionalOutput(average, totalWeight, desiredGrade);
}
}
if (localStorage.getItem("darkMode") === null) {
localStorage.setItem("darkMode", "true");
}
function applyDarkTheme() {
const bodyElement = document.body;
const contentWrapper = document.querySelectorAll('.content_wrapper');
const pElement = document.querySelectorAll('p');
const h2Element = document.querySelectorAll('h2');
const labelElement = document.querySelectorAll('label');
const addRowBtn = document.querySelector('#addRowBtn');
const toggleStatus = document.querySelector('#toggleStatus');
bodyElement.style.backgroundColor = "rgb(30, 30, 30)";
contentWrapper.forEach(wrapper => {
wrapper.style.backgroundColor = "rgb(45, 45, 45)";
});
pElement.forEach(p => {
p.style.color = "white";
});
h2Element.forEach(h2 => {
h2.style.color = "white";
});
labelElement.forEach(label => {
label.style.color = "white";
});
addRowBtn.style.color = "white";
toggleStatus.style.backgroundColor = "rgb(21, 9, 73)";
toggleStatus.style.color = "white";
toggleStatus.textContent = "Dark Mode: ON";
}
function applyLightTheme() {
const bodyElement = document.body;
const contentWrapper = document.querySelectorAll('.content_wrapper');
const pElement = document.querySelectorAll('p');
const h2Element = document.querySelectorAll('h2');
const labelElement = document.querySelectorAll('label');
const addRowBtn = document.querySelector('#addRowBtn');
const toggleStatus = document.querySelector('#toggleStatus');
bodyElement.style.backgroundColor = "rgb(255, 255, 255)";
contentWrapper.forEach(wrapper => {
wrapper.style.backgroundColor = "rgb(200, 200, 200)";
});
pElement.forEach(p => {
p.style.color = "black";
});
h2Element.forEach(h2 => {
h2.style.color = "black";
});
labelElement.forEach(label => {
label.style.color = "black";
});
addRowBtn.style.color = "black";
toggleStatus.style.backgroundColor = "rgb(251, 209, 211)";
toggleStatus.style.color = "black";
toggleStatus.textContent = "Dark Mode: OFF";
}
function initToggle() {
if (JSON.parse(localStorage.getItem("darkMode"))) {
applyDarkTheme();
} else {
applyLightTheme();
}
}
function toggleDark() {
let darkModeValue = JSON.parse(localStorage.getItem("darkMode"));
darkModeValue = !darkModeValue; // Toggle the value (if it was true, now it becomes false, and vice versa)
localStorage.setItem("darkMode", JSON.stringify(darkModeValue)); // Save the updated value in localStorage
initToggle();
}
// Initialization
// initAccordion();
initRows();
initToggle();