-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (50 loc) · 1.97 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
// Credits information for each semester
// credits are in credits.js file in the same directory
// import them ans use them in the code
// Define credits
import SemToCredits from "./credits.js";
import gradePoints from "./gradePoints.js";
const semesterInput = document.getElementById("semester");
const calculateButton = document.getElementById("calculate");
semesterInput.addEventListener("change", renderSubjectInputs);
window.addEventListener("load", renderSubjectInputs);
calculateButton.addEventListener("click", calculateSGPA);
// Function to dynamically render subject inputs
function renderSubjectInputs() {
let credits = SemToCredits[parseInt(semesterInput.value)];
const subjectInputsContainer = document.getElementById("subjectInputs");
subjectInputsContainer.innerHTML = ""; // Clear previous inputs
for (const subject in credits) {
const label = document.createElement("label");
label.textContent = `${subject} (Credits: ${credits[subject]})`;
const select = document.createElement("select");
select.id = subject;
// Populate options
for (const grade in gradePoints) {
const option = document.createElement("option");
option.value = grade.toLowerCase();
option.textContent = grade;
select.appendChild(option);
}
subjectInputsContainer.appendChild(label);
subjectInputsContainer.appendChild(select);
}
}
function calculateSGPA() {
let totalCredits = 0;
let totalGradePoints = 0;
const credits = SemToCredits[parseInt(semesterInput.value)];
// Loop through each subject
for (const subject in credits) {
const gradeInput = document.getElementById(subject).value.toUpperCase();
const gradePoint = gradePoints[gradeInput];
if (gradePoint !== undefined) {
totalGradePoints += gradePoint * credits[subject];
totalCredits += credits[subject];
}
}
const SGPA = totalGradePoints / totalCredits;
document.getElementById("result").innerHTML = `Your SGPA is: ${SGPA.toFixed(
2
)}`;
}