-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.babel
More file actions
328 lines (305 loc) · 12 KB
/
script.babel
File metadata and controls
328 lines (305 loc) · 12 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
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
326
327
328
/*Manager: in charge of functionalities of program;
in charge of storing data needed for program as a whole*/
class Manager extends React.Component {
constructor(props){
super(props);
this.state = { //Manager states
savedStudents:[{
name: "Adam", math: "A", history: "B", science: "A", english: "B",
gpa: "3.50", selected: false
},
{
name: "John", math: "B", history: "B", science: "B", english: "B",
gpa: "3.00", selected: false
},
{
name: "Will", math: "C", history: "B", science: "B", english: "C",
gpa: "2.50", selected: false
},
{
name: "Alex", math: "D", history: "B", science: "C", english: "B",
gpa: "2.25", selected: false
},
{
name: "Phil", math: "F", history: "B", science: "C", english: "D",
gpa: "1.50", selected: false
}],
highestGPA: [0], // list of index to state
lowestGPA: [4], //list of index to state
}
this.currentStudentData = { //template to create student datas
name: "",
math: "",
history: "",
science: "",
english: "",
gpa: "",
};
//Manager functions; binding
this.submitStudentInput = this.submitStudentInput.bind(this);
this.displayRender = this.displayRender.bind(this);
this.resetCurrentStudentData = this.resetCurrentStudentData.bind(this);
this.calculatePts = this.calculatePts.bind(this);
this.initStudentGrade = this.initStudentGrade.bind(this);
this.convertInput = this.convertInput.bind(this);
this.convertCapital = this.convertCapital.bind(this);
this.addExtremes = this.addExtremes.bind(this);
this.studentDataSelected = this.studentDataSelected.bind(this);
this.removeStudent = this.removeStudent.bind(this);
this.filterToKeep = this.filterToKeep.bind(this);
this.reEval = this.reEval.bind(this);
}
//--------------------------------FUNCTIONS:---------------------------------------
//--------------------adding student data:-----------------------------------
submitStudentInput(){
this.currentStudentData.math = this.initStudentGrade(this.currentStudentData.math);
this.currentStudentData.history = this.initStudentGrade(this.currentStudentData.history);
this.currentStudentData.science = this.initStudentGrade(this.currentStudentData.science);
this.currentStudentData.english = this.initStudentGrade(this.currentStudentData.english);
this.currentStudentData.selected = false;
var currentGPA = this.calculatePts([this.currentStudentData.math,
this.currentStudentData.history,
this.currentStudentData.science, this.currentStudentData.english]);
if(currentGPA == -1){ // invalid input
this.resetCurrentStudentData();
return;
}
this.currentStudentData.gpa = (parseFloat(currentGPA)).toFixed(2);
//------if input is valid-------------------------------------
const localSavedStudents = this.state.savedStudents.slice();
localSavedStudents.push(Object.assign({}, this.currentStudentData));
this.setState({savedStudents: localSavedStudents}, ()=> {this.addExtremes()}); // callback to prevent delay in changing state
this.resetCurrentStudentData();
}
calculatePts(subjectGrades){ // calculate student GPA based off of input
var total = 0;
for(var i = 0; i < subjectGrades.length; i++){
switch (subjectGrades[i]){
case "A":
total+=4;
break;
case "B":
total+=3;
break;
case "C":
total+=2;
break;
case "D":
total+=1;
break;
case "F":
total+=0;
break;
default:
alert("invalid grade input/s");
return -1; //error
}
}
return (total/subjectGrades.length);
}
convertInput(input){ //converts input into a letter grade for testing
if(Number(input) >= 90){
return "A";
}
if(Number(input) >= 80 && Number(input) < 90){
return "B";
}
if(Number(input) >= 70 && Number(input) < 80){
return "C";
}
if(Number(input) >= 60 && Number(input) < 50){
return "D";
}
return "F";
}
convertCapital(input){ // turn lower case grade inputs as upper case
return input.toUpperCase();
}
initStudentGrade(input){ // decides how to convert grade for easier calculation
if(Number(input) != NaN && Number(input) > 0){
return this.convertInput(input);
}else{
return this.convertCapital(input);
}
}
//---------reset studentData; prep for next insertion----
resetCurrentStudentData(){
this.currentStudentData = {
name: "",
math: "",
history: "",
science: "",
english: "",
gpa: "",
};
this.refs.nameInput.value = "";
this.refs.mathInput.value = "";
this.refs.historyInput.value = "";
this.refs.scienceInput.value = "";
this.refs.englishInput.value = "";
}
//---------------------handling min and max coloring------------------------------
addExtremes(){ // determines if new data is a max or a min
if(this.state.savedStudents.length > 1){
if(Number(this.state.savedStudents[this.state.savedStudents.length - 1].gpa) ==
Number(this.state.savedStudents[this.state.highestGPA[0]].gpa)){
const highestArr = this.state.highestGPA.slice();
highestArr.push(this.state.savedStudents.length - 1);
this.setState({highestGPA: highestArr});
}
else if(Number(this.state.savedStudents[this.state.savedStudents.length - 1].gpa) ==
Number(this.state.savedStudents[this.state.lowestGPA[0]].gpa)){
const lowestArr = this.state.lowestGPA.slice();
lowestArr.push(this.state.savedStudents.length - 1);
this.setState({lowestGPA: lowestArr});
}
else if( Number(this.state.savedStudents[this.state.savedStudents.length - 1].gpa) >
Number(this.state.savedStudents[this.state.highestGPA[0]].gpa)){
const highestArr = [(this.state.savedStudents.length - 1)];
this.setState({highestGPA: highestArr});
}
else if (Number(this.state.savedStudents[this.state.savedStudents.length - 1].gpa) <
Number(this.state.savedStudents[this.state.lowestGPA[0]].gpa)){
const lowestArr = [(this.state.savedStudents.length - 1)];
this.setState({lowestGPA: lowestArr});
}
}
else{
const elementArr= [(this.state.savedStudents.length - 1)];
this.setState({lowestGPA: elementArr});
this.setState({highestGPA: elementArr});
}
}
//---removing student data-----------------------------------
studentDataSelected(index){
const savedArr = this.state.savedStudents.slice();
savedArr[index].selected = !(savedArr[index].selected);
this.setState({savedStudents : savedArr});
}
removeStudent(){
const studentsToKeep = this.state.savedStudents.slice();
const editedStudentArr = studentsToKeep.filter(this.filterToKeep);
this.setState({savedStudents: editedStudentArr}, ()=>{this.reEval();});
}
reEval(){ //reevaluate min and max gpas after removing elements
var lowestIndex = [];
var lowestVal = 4;
var highestIndex = [];
var highestVal = 0;
for(var i = 0; i < this.state.savedStudents.length; i++){
if(highestVal < Number(this.state.savedStudents[i].gpa)){
highestVal = Number(this.state.savedStudents[i].gpa);
highestIndex = [i];
}
else if (highestVal == Number(this.state.savedStudents[i].gpa)){
highestIndex.push(i);
}
if(lowestVal > Number(this.state.savedStudents[i].gpa)){
lowestVal = Number(this.state.savedStudents[i].gpa);
lowestIndex= [i];
}
else if (lowestVal == Number(this.state.savedStudents[i].gpa)){
lowestIndex.push(i);
}
}
this.setState({lowestGPA: lowestIndex});
this.setState({highestGPA: highestIndex});
}
filterToKeep(currStudent){
return !(currStudent.selected);
}
//-------------------displaying the saved data-------------------------------
displayRender(i){
var cusStyle = {
backgroundColor: 'rgba(10, 60, 190, 0.2)'
};
if(this.state.highestGPA.includes(i)){
cusStyle = {
backgroundColor: 'rgba(80,255,80, 1)'
};
}
else if(this.state.lowestGPA.includes(i)){
cusStyle = {
backgroundColor: 'rgba(255,60,60, 1)',
};
}
if(this.state.savedStudents[i].selected){
cusStyle = {
border: '2px solid blue'
};
}
return <StudentDataDisplay
style = {cusStyle}
onClick={()=>{this.studentDataSelected(i);}}
name={this.state.savedStudents[i].name}
mathGrade={this.state.savedStudents[i].math}
historyGrade={this.state.savedStudents[i].history}
scienceGrade={this.state.savedStudents[i].science}
englishGrade={this.state.savedStudents[i].english}
gpa={this.state.savedStudents[i].gpa}
/>;
}
//end Functions------------------------------------------------------------
render() { //create student data based on what's in savedStudents
const defaultStudentData=[];
for(var i = 0; i < this.state.savedStudents.length; i++){
defaultStudentData.push(this.displayRender(i));
}
return ( //HTML contents:
<div className="outermost-window">
<div className="title">STUDENT GRADE STORAGE</div>
<div className="manager-body">
<div className="body-headers">
<label>Name</label>
<label>Math</label>
<label>History</label>
<label>Science</label>
<label>English</label>
<label>GPA</label>
</div>
{defaultStudentData}
</div>
<div className="manager-input">
<input type='text' ref="nameInput" placeHolder="enter student name" onChange={(e)=>{
this.currentStudentData.name = e.target.value;
}}/>
<input type='text' ref="mathInput" placeHolder="enter math letter grade" onChange={(e)=>{
this.currentStudentData.math = e.target.value;
}}/>
<input type='text' ref="historyInput" placeHolder="enter history letter grade" onChange={(e)=>{
this.currentStudentData.history = e.target.value;
}}/>
<input type='text' ref="scienceInput" placeHolder="enter science letter grade" onChange={(e)=>{
this.currentStudentData.science = e.target.value;
}}/>
<input type='text' ref="englishInput" placeHolder="enter english letter grade" onChange={(e)=>{
this.currentStudentData.english = e.target.value;
}}/>
<button className="input-button" onClick={this.submitStudentInput}> Add Student</button>
<button className="input-button" onClick={this.removeStudent}>Remove Student</button>
</div>
</div>
);
}
}
//template to render stored student data
class StudentDataDisplay extends React.Component{
render(){
return(
<div className="student-data" style={this.props.style} onClick={this.props.onClick}>
<label>{this.props.name}</label>
<label>{this.props.mathGrade}</label>
<label>{this.props.historyGrade}</label>
<label>{this.props.scienceGrade}</label>
<label>{this.props.englishGrade}</label>
<label>{this.props.gpa}</label>
</div>
);
}
}
//=========================================================
//connect react script to index
ReactDOM.render(
<Manager />,
document.getElementById('window')
);