-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrade-calculator-script.js
More file actions
115 lines (101 loc) · 3.91 KB
/
grade-calculator-script.js
File metadata and controls
115 lines (101 loc) · 3.91 KB
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
document.addEventListener("DOMContentLoaded", () => {
document.getElementById('desired-grade').addEventListener('change', handleDesiredGrade);
});
function searchSubject() {
const year = document.getElementById('year').value;
const subjectCode = document.getElementById('subject-code').value;
const searchUrl = `https://apps.jcu.edu.au/subjectsearch/#/subject/${year}/${subjectCode}`;
window.open(searchUrl, '_blank');
}
function handleDesiredGrade() {
const input = document.getElementById('desired-grade').value.toUpperCase();
let desiredGrade;
if (input.endsWith('%')) {
desiredGrade = parseFloat(input);
} else {
switch(input) {
case 'P': desiredGrade = 50; break;
case 'C': desiredGrade = 65; break;
case 'D': desiredGrade = 75; break;
case 'HD': desiredGrade = 85; break;
default:
desiredGrade = parseFloat(input);
}
}
if (isNaN(desiredGrade) || desiredGrade < 0 || desiredGrade > 100) {
alert('Please enter a valid grade.');
return null;
}
return desiredGrade;
}
function addRow() {
const table = document.getElementById('grade-table').getElementsByTagName('tbody')[0];
const newRow = table.insertRow();
const itemCell = newRow.insertCell(0);
const weightCell = newRow.insertCell(1);
const gradeCell = newRow.insertCell(2);
const removeCell = newRow.insertCell(3);
itemCell.innerHTML = '<input type="text" class="item-input">';
weightCell.innerHTML = '<input type="number" class="weight-input">';
gradeCell.innerHTML = '<input type="number" class="grade-input" placeholder="out of 100%">';
removeCell.innerHTML = '<button onclick="removeRow(this)">-</button>';
}
function removeRow(button) {
const row = button.parentNode.parentNode;
row.parentNode.removeChild(row);
}
function calculateRest() {
const weightInputs = document.querySelectorAll('.weight-input');
const gradeInputs = document.querySelectorAll('.grade-input');
let totalWeight = 0;
let currentGrade = 0;
let emptyWeights = 0;
let desiredGrade = handleDesiredGrade();
if (desiredGrade === null) {
return; // Stop calculation if desired grade is not valid
}
weightInputs.forEach((weightInput, index) => {
const weight = parseFloat(weightInput.value);
let gradeValue = gradeInputs[index].value;
// Check if grade is in x/y format and convert to percentage
if (gradeValue.includes('/')) {
const parts = gradeValue.split('/');
if (parts.length === 2) {
const numerator = parseFloat(parts[0]);
const denominator = parseFloat(parts[1]);
if (!isNaN(numerator) && !isNaN(denominator) && denominator !== 0) {
gradeValue = (numerator / denominator) * 100;
} else {
gradeValue = NaN; // Invalid format or division by zero, treat as NaN
}
} else {
gradeValue = NaN; // Invalid format, treat as NaN
}
} else {
gradeValue = parseFloat(gradeValue);
}
if (!isNaN(weight)) {
totalWeight += weight;
if (!isNaN(gradeValue)) {
currentGrade += (weight / 100) * gradeValue;
} else {
emptyWeights += weight; // Track the sum of the weights for empty grade fields
}
}
});
if (totalWeight !== 100) {
document.getElementById('error-message').textContent = 'Error: The sum of the weightings must equal 100.';
return;
}
document.getElementById('error-message').textContent = ''; // Clear error message if weights are valid
gradeInputs.forEach((input, index) => {
if (input.value === '') {
const weight = parseFloat(weightInputs[index].value);
if (!isNaN(weight)) {
const calculatedGrade = 100 * (desiredGrade - currentGrade) / emptyWeights;
input.value = Math.max(0, calculatedGrade).toFixed(2); // Ensure grades aren't negative
input.style.color = 'red';
}
}
});
}