+ 2

JavaScript - Need Help! Arrays and Loops.

I am beating my head against the wall trying to figure out how to add a loop to my JavaScript Grade Sheet I just made. I want to loop through each students grade that's been input and then add all the students grades together to get a total GPA for the class. I think I need to add an array and loop through the array and then add everything together, but I'm just not sure how to do this. Code is here https://code.sololearn.com/WSarwyNGeSP1/?ref=app What I am asking will probably be easier to figure out once you see my code.

3rd Jun 2018, 7:38 AM
Sushi_Theif
Sushi_Theif - avatar
12 ответов
+ 5
Looking over your code again, doing: if(grade1<50) { grade1 = 0; } else if(grade1<60) { grade1 = 1.0; } else if(grade1<70) { grade1 = 2.0; } etc. That is tedious. Because you did thar for every grade. Now if you have 100 grades, you will have to do that 100 times!? MADNESS. simply put them all in an array: for(int x=0; x<grades.length; x++) { if(grades[x]<50) { grades[x] = 0; } else if(grades[x]<60) { grades[x] = 1.0; } else if(grades[x]<70) { grades[x] = 2.0; } etc } This way you do the tedipjs grade setting once, but afterwards no matter how many students grades you add after, you don't need to copy and paste that long code and change the number. It will all be handled in that loop. This is the way I know how to do it. I do not know enough Javascript to code it, so I put it as simple as possible. Best of luck programming Blaine P ! ☺
3rd Jun 2018, 8:41 AM
Andre Daniel
Andre Daniel - avatar
+ 2
Okay I am getting confused like this? grade1 = grade[0]; grade2 = grade[1]; grade3 = grade[2];
3rd Jun 2018, 8:47 AM
Sushi_Theif
Sushi_Theif - avatar
+ 1
Blaine P Solved! ☺ It concatenates the GPAs because they are all strings. You will need to conveet them to floats. I have added this to the bottom of the code: result = (parseFloat(grade1) + parseFloat( grade2) + parseFloat( grade3) + parseFloat( grade4) + parseFloat( grade5))/5; document.getElementById("totalGPA").innerHTML = result; Aside from the long decimal point line, this code works.
3rd Jun 2018, 8:13 AM
Andre Daniel
Andre Daniel - avatar
+ 1
This works awesome! You are a genius :) Now i am still unsure of how to implement a loop into my code. So, it loops through each grade and calculates it as it runs through the loop.
3rd Jun 2018, 8:25 AM
Sushi_Theif
Sushi_Theif - avatar
+ 1
Thanks for your help, ill keep trying to crank away.
3rd Jun 2018, 8:54 AM
Sushi_Theif
Sushi_Theif - avatar
0
Add your code to this question right here so everyone can access it in one tap without going through the trouble of looking for it, which may turn them off from answering altogether. Are you saying you are unsure of how to create and array and create a for loop?
3rd Jun 2018, 7:48 AM
Andre Daniel
Andre Daniel - avatar
0
Blaine P are you talking about this? https://code.sololearn.com/WSarwyNGeSP1/?ref=app It seems as though you have coded a way for the individual GPAs. I'd assume to get the total for the class, you just take the average of those 5, or sum it. (I don't know how GPAs work)
3rd Jun 2018, 7:55 AM
Andre Daniel
Andre Daniel - avatar
0
Heres Code https://code.sololearn.com/WSarwyNGeSP1/# I know how to create an array and a loop, i just dont know how to apply that to looping through each students grades on my sheet and then adding it all together.
3rd Jun 2018, 7:56 AM
Sushi_Theif
Sushi_Theif - avatar
0
so I tried something like this at the bottom of my function var Total = grade1 + " " + grade2 + " " + grade3 + " " + grade4 + " " + grade5; document.getElementById("totalGPA").innerHTML = Total; but this will only concatenate the GPAs together, not actually add them. This also doesn't help me incorporate a loop, which is really what I want to know how to do.
3rd Jun 2018, 8:05 AM
Sushi_Theif
Sushi_Theif - avatar
0
Looping is a different thing entirely. You will have to store the grades in the arrays instead (grade1 will go in gradeArray[0] etc) anda for loop can add up the values, once again if you parse each of them to floats, and then divide for the average (if that is required). For a program with only 5 students, it is not neccessary to implement, but would indeed be beneficial for an entire school.
3rd Jun 2018, 8:22 AM
Andre Daniel
Andre Daniel - avatar
0
Yeah I know it doesn't make much sense here, but I am trying to implement it anyway so I can see how it would work. I initially had something like this, var GradesArray = ["grade1", "grade2", "grade3", "grade4", "grade5"]; var arrayLength = GradesArray.length; for (var i = 0; i < arrayLength; i++){ //Unsure of what to do here } but I'm not sure if I am even setting this up correctly.
3rd Jun 2018, 8:33 AM
Sushi_Theif
Sushi_Theif - avatar
0
At the beginning I would create something like: grades[] = new float(5); //defines 5 arrays to hold the 5 grades. Then, where you have: grade1 = etc; Put grades[0] = etc; Since you say you know arrays, I assume you will know to change grade2 to grades[1] and grade3 to grades[2] etc. For adding them with a for loop, I will use: total = 0; for(int x=0; x<grades.length; x++) { total += parseFloat(grades[x]); } totalavg = total/5; document.getElementById("totalGPA").innerHTML = totalavg; Blaine P
3rd Jun 2018, 8:33 AM
Andre Daniel
Andre Daniel - avatar