This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradecalc.js
170 lines (151 loc) · 6.24 KB
/
gradecalc.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
// ------------ Find Elements ------------ //
//ID Sections
var entryBoxes = document.getElementById("entryBoxes");
var assignmentsTable = document.getElementById("assignmentsTable");
var overallGrade = document.getElementById("overallGrade");
var tBody = document.getElementById("tBody");
var errorMSG = document.getElementById("errorMSG");
//Text Boxes
var assignmentName = document.getElementById("assignmentName");
var pointsEarned = document.getElementById("pointsEarned");
var pointsPossible = document.getElementById("pointsPossible");
//Buttons
var addAssignment = document.getElementById("addAssignment");
var cancelAdd = document.getElementById("cancelAdd");
var newAssignment = document.getElementById("newAssignment");
var removeAll = document.getElementById("removeAll");
//Button Groups
var addMainButtons = document.getElementById("addMainButtons");
var mainButtons = document.getElementById("mainButtons");
//Assignements Array
var Assignments = [];
// ------------ Functions ------------ //
// Clears Inputs
var clearInputs = function() {
assignmentName.value = "";
pointsEarned.value = "";
pointsPossible.value = "";
errorMSG.setAttribute("style", "display:none;");
};
// Only Allows Numbers for Specific Inputs
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode > 31 && (charCode != 46 && (charCode < 48 || charCode > 57)))
return false;
return true;
}
// Renders Assignments Table
var renderAssignments = function() {
//Clears some stuff...
clearInputs();
tBody.innerHTML = "<tr><th>Assignment</th><th>Points Earned</th><th>Points Total</th><th>Percent</th><th>Remove/Edit</th></tr>";
//Setup for Overall Grade
var overallEarned = 0;
var overallPossible = 0;
//RENDER ASSIGNMENTS
for (var i = 0; i < Assignments.length; i++) {
//Add it to the assignments page
var trTag = document.createElement("tr");
//Gets percent for Assignment
var assignmentPercent = (Assignments[i].earned / Assignments[i].possible) * 100;
//Rounds
var roundedPercent = assignmentPercent.toFixed(2);
trTag.innerHTML = "<td>" + Assignments[i].name + "</td>" + "<td>" + Assignments[i].earned + "</td>" + "<td>" + Assignments[i].possible + "</td><td>" + roundedPercent + "%</td><td><button id='removeButton" + [i] +
"'>X</button><button id='editButton" + [i] + "'>Edit</button></td>";
tBody.appendChild(trTag);
trTag.setAttribute("class", "tableTR");
trTag.setAttribute("id", "num" + [i]);
//Adds values onto variable
overallEarned += +Assignments[i].earned;
overallPossible += +Assignments[i].possible;
//Setup for Remove Single
var removeSingleButton = document.getElementById("removeButton" + i);
removeSingleButton.addEventListener("click", removeSingle);
//Setup for Edit
var editSingleButton = document.getElementById("editButton" + i);
editSingleButton.addEventListener("click", editSingle);
}
//Gets Percent for Overall
var overallGradePercent = (overallEarned / overallPossible) * 100;
//Rounds
var overallRoundedPercent = overallGradePercent.toFixed(0);
if (isNaN(overallRoundedPercent)) {
overallRoundedPercent = 0;
}
overallGrade.innerHTML = "Overall Grade: " + overallRoundedPercent + "%";
};
var newAssignmentFunction = function(event) {
//Hide Assignments List and Show Add Assignment
entryBoxes.setAttribute("style", "display:block;");
assignmentsTable.setAttribute("style", "display:none;");
overallGrade.setAttribute("style", "display:none;");
mainButtons.setAttribute("style", "display:none;");
document.addEventListener("keydown", enterPress);
};
var addAssignmentFunction = function(event) {
//If everything is filled out...
if (assignmentName.value.length >= 1 && pointsEarned.value.length >= 1 && pointsPossible.value.length >= 1 && pointsPossible.value !== 0) {
//Add Assignment to Array
Assignments.push({
name: assignmentName.value,
earned: pointsEarned.value,
possible: pointsPossible.value
});
//Show Assignments List and Hide Add Assignment
entryBoxes.setAttribute("style", "display:none;");
assignmentsTable.setAttribute("style", "display:inline-block;");
overallGrade.setAttribute("style", "display:block;");
mainButtons.setAttribute("style", "display:block;");
document.removeEventListener("keydown", enterPress);
//Render List
renderAssignments();
} else {
errorMSG.setAttribute("style", "display:block;");
}
};
cancelAddFunction = function(event) {
entryBoxes.setAttribute("style", "display:none;");
assignmentsTable.setAttribute("style", "display:inline-block;");
overallGrade.setAttribute("style", "display:block;");
mainButtons.setAttribute("style", "display:block;");
clearInputs();
};
// Removes all assignments from the list
removeAllFunction = function() {
Assignments = [];
renderAssignments();
};
//Removes a Single Item
var removeSingle = function() {
//Gets Specific Instance
var buttonId = event.currentTarget.getAttribute("ID");
var buttonNum = buttonId.slice(-1);
//Removes Item from Array
Assignments.splice(buttonNum, 1);
renderAssignments();
};
//Edits an Assignment
var editSingle = function() {
//Gets Specific Instance
var buttonId = event.currentTarget.getAttribute("ID");
var buttonNum = buttonId.slice(-1);
//Gets Values and Puts in Boxes
assignmentName.value = Assignments[buttonNum].name;
pointsEarned.value = Assignments[buttonNum].earned;
pointsPossible.value = Assignments[buttonNum].possible;
//Shows Input Boxes
newAssignmentFunction();
Assignments.splice(buttonNum, 1);
};
// Pressing Enter Adds Item to List
var enterPress = function(event) {
if (event.keyCode == 13) {
addAssignmentFunction();
}
};
// ------------ Event Listeners ------------ //
newAssignment.addEventListener("click", newAssignmentFunction);
addAssignment.addEventListener("click", addAssignmentFunction);
cancelAdd.addEventListener("click", cancelAddFunction);
removeAll.addEventListener("click", removeAllFunction);
renderAssignments();