Trouble calculating 2 total sums in JavaScript

  Post new topicReply to topicPrintable Version
<< View previous topic View next topic >>
Share: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
#1   Trouble calculating 2 total sums in JavaScript
Gozen
CZ Newbie
Gozen has been a member for over 19 year's 19 Year Member
Gender: Male
Status: Offline
Joined: Jan 28, 2005
0.00 posts per day
Posts: 7
Points: 455
   
I'm having trouble calculating the totalgradepts and totalcredits located toward the bottom of this code. I need each of them to calculate the sum of all the information the users inputs. I'm stumped on what to do. Any help is appreciated. Thanks!



var i=1, x, course, credits, grade, gradepts, totalgradepts, totalcredits, gpa;

x = window.prompt("Please enter the total number of courses you are taking this semester: ", "0");

document.write("<table border = 1> <tr><td>Course</td> <td>Credits</td> <td>Grade</td> <td>Grade

Points
</td></tr>");

for(i; i<=x; i++) {
course = window.prompt("Please enter the name of a course you are taking: ", "Course");
credits = window.prompt("Please enter the amount of credits that course is worth: ", "3");
grade = window.prompt("Please enter the letter grade you received for the course: ", "A");


switch (grade) {
case "A":
gradepts = (4.00 * credits);
break;

case "A-":
gradepts = (3.66 * credits);
break;

case "B+":
gradepts = (3.33 * credits);
break;

case "B":
gradepts = (3.00 * credits);
break;

case "B-":
gradepts = (2.66 * credits);
break;

case "C+":
gradepts = (2.33 * credits);
break;

case "C":
gradepts = (2.00 * credits);
break;

case "D":
gradepts = (1.00 * credits);
break;

case "F":
gradepts = (0.00 * credits);
break;

default:
validInput = false;
}

document.write("<tr><td>" + course + "</td><td>" + credits + "</td><td>" + grade + "</td><td>" + gradepts + "</tr> ") ;

totalgradepts = gradepts;
totalcredits = credits+credits+credits;

gpa = (totalgradepts/totalcredits);

document.write("<tr><td colspan='4'> ;;</td></tr><tr><td colspan='4'>Total Grade Points: " + totalgradepts +

"</td></tr> <tr><td colspan='4'>GPA: " + gpa + "</td></tr></table>");



Back to top Reply with quote
#2   re: Trouble calculating 2 total sums in JavaScript
GoddsEgo
PayPal Donation
CZ Moderator
Status: Offline
Joined: Jun 26, 2003
0.15 posts per day
Posts: 1211
Points: 69,166
 Yahoo Messenger  
Hi Gozen,
Welcome to Cz.

I am not quite sure how you are trying to calculate all this but what I figured was...

course credit * the value of your letter gade = grade pt

So if you took two courses and the first course is worth 2 credits and you got an A which has a value of 4.00
then we have

2 * 4.00 = 8 grade pts

second course worth 3 credits and you got an A which has a value of 4.00
we have

3 * 4.00 = 12 grade pts

total grade points would be

8 + 12 = 20 total grade points

then we get the average by taking total grade pts and dividing it by the number of courses I took which would be

20 / 2 = 10gpa


Looks wrong to me but if its right then here's the script...

var i=1, x, course, credits, grade, gradepts, totalgradepts, totalcredits, gpa;
var subtotalgradepts = 0;
x = window.prompt("Please enter the total number of courses you are taking this semester: ", "0");

document.write("<table border = 1> <tr><td>Course</td> <td>Credits</td> <td>Grade</td> <td>Grade Points</td></tr>");

for(i; i<=x; i++) {
course = window.prompt("Please enter the name of a course you are taking: ", "Course");
credits = window.prompt("Please enter the amount of credits that course is worth: ", "0");
grade = window.prompt("Please enter the letter grade you received for the course: ", "A");


switch (grade) {
case "A":
gradepts = (4.00 * credits);
break;

case "A-":
gradepts = (3.66 * credits);
break;

case "B+":
gradepts = (3.33 * credits);
break;

case "B":
gradepts = (3.00 * credits);
break;

case "B-":
gradepts = (2.66 * credits);
break;

case "C+":
gradepts = (2.33 * credits);
break;

case "C":
gradepts = (2.00 * credits);
break;

case "D":
gradepts = (1.00 * credits);
break;

case "F":
gradepts = (0.00 * credits);
break;

default:
validInput = false;
}
subtotalgradepts += gradepts;
totalgradepts = subtotalgradepts;
document.write("<tr><td>" + course + "</td><td>" + credits + "</td><td>" + grade + "</td><td>" + gradepts + "</tr> ") ;
}
gpa = (totalgradepts/x);
document.write("<tr><td colspan='4'> ;;</td></tr><tr><td colspan='4'>Total Grade Points: " + totalgradepts + "</td></tr> <tr><td colspan='4'>GPA: " + gpa + "</td></tr></table>");



Back to top Reply with quote
#3   re: Trouble calculating 2 total sums in JavaScript
Gozen
CZ Newbie
Gozen has been a member for over 19 year's 19 Year Member
Gender: Male
Status: Offline
Joined: Jan 28, 2005
0.00 posts per day
Posts: 7
Points: 455
   
Thanks GoddsEgo, it all seems good except for the gpa. The way you solve to calculate the totalgradepts is great and is correct, but for the gpa I have to figure a way to keep track of the credits entered for each course. For example if you are taking 3 courses total and one course is worth 3 credits, another 4 credits and another course worth 1 that would total 8 credits which is the other thing that I need to calculate. Once total credits are calculated you would take the totalgradepts which you calculated and divide by the total number of credits you are taking. I'm trying to figure it out now by using the same technique which you used to calculate the totalgradepts but am unable to get correct results. Thanks again!



Back to top Reply with quote
#4   re: Trouble calculating 2 total sums in JavaScript
Gozen
CZ Newbie
Gozen has been a member for over 19 year's 19 Year Member
Gender: Male
Status: Offline
Joined: Jan 28, 2005
0.00 posts per day
Posts: 7
Points: 455
   
I got it working but I'm not sure if there is a better or more efficient way than this. All I did was add another switch statement and just assumed values that the user could possibly enter.

If it is possible to somehow calculate the total number of credits using the value of the 'y' variable without having to use a switch statement, please let me know.

var i=1, x, course, y, credits, grade, gradepts, totalgradepts, totalcredits, gpa, newgpa;
var subtotalgradepts = 0, subtotalcredits = 0;

x = window.prompt("Please enter the total number of courses you are taking this semester: ",

"0");

document.write("<table border = 1> <tr><td>Course</td> <td>Credits</td> <td>Grade</td> <td>Grade

Points</td></tr>");

for(i; i<=x; i++) {
course = window.prompt("Please enter the name of a course you are taking: ", "Course");
y = window.prompt("Please enter the amount of credits that course is worth: ", "3");
grade = window.prompt("Please enter the letter grade you received for the course: ", "A");

switch (y) {
case ".5":
credits = (.5);
break;

case "1":
credits = (1);
break;

case "1.5":
credits = (1.5);
break;

case "2":
credits = (2);
break;

case "2.5":
credits = (2.5);
break;

case "3":
credits = (3);
break;

case "3.5":
credits = (3.5);
break;

case "4":
credits = (4);
break;

default:
validInput = false;
}

switch (grade) {
case "A":
gradepts = (4.00 * y);
break;

case "A-":
gradepts = (3.66 * y);
break;

case "B+":
gradepts = (3.33 * y);
break;

case "B":
gradepts = (3.00 * y);
break;

case "B-":
gradepts = (2.66 * y);
break;

case "C+":
gradepts = (2.33 * y);
break;

case "C":
gradepts = (2.00 * y);
break;

case "D":
gradepts = (1.00 * y);
break;

case "F":
gradepts = (0.00 * y);
break;

default:
validInput = false;
}
subtotalgradepts += gradepts;
totalgradepts = subtotalgradepts;

subtotalcredits += credits;
totalcredits = subtotalcredits;

document.write("<tr><td>" + course + "</td><td>" + credits + "</td><td>" + grade + "</td><td>" +

gradepts + "</tr> ") ;
}

gpa = (totalgradepts/totalcredits);
newgpa = Math.round(gpa * 100)/100;
document.write("<tr><td colspan='4'>  ;;</td></tr><tr><td colspan='4'>Total Grade Points: " +

totalgradepts + "</td></tr> <tr><td colspan='4'>GPA: " + newgpa + "</td></tr></table>");
document.writeln(totalcredits);



Back to top Reply with quote
#5   re: Trouble calculating 2 total sums in JavaScript
Gozen
CZ Newbie
Gozen has been a member for over 19 year's 19 Year Member
Gender: Male
Status: Offline
Joined: Jan 28, 2005
0.00 posts per day
Posts: 7
Points: 455
   
Sorry for all the posts, but I'm all set now and figured out what to do on my own. It appears to work all good and does what it's supposed to.
var i=1, x, y, course, credits, grade, gradepts, totalgradepts, totalcredits, gpa, totalsumcredits = 0, totalsumgradepts = 0;

x = window.prompt("Please enter the total number of courses you are taking this semester: ", "0");

document.write("<table border = 1> <tr><td><b>Course</b></td> <td><b>Credits</b></td> <td><b>Grade</b></td> <td><b>Grade

Points</b></td></tr>");

for(i; i<=x; i++) {
course = window.prompt("Please enter the name of a course you are taking: ", "Course");
y = window.prompt("Please enter the amount of credits that course is worth: ", "3");
credits = parseInt(y);
grade = window.prompt("Please enter the letter grade you received for the course: ", "A");


switch (grade) {
case "A":
gradepts = (4.00 * credits);
break;

case "A-":
gradepts = (3.66 * credits);
break;

case "B+":
gradepts = (3.33 * credits);
break;

case "B":
gradepts = (3.00 * credits);
break;

case "B-":
gradepts = (2.66 * credits);
break;

case "C+":
gradepts = (2.33 * credits);
break;

case "C":
gradepts = (2.00 * credits);
break;

case "D":
gradepts = (1.00 * credits);
break;

case "F":
gradepts = (0.00 * credits);
break;

default:
validInput = false;
}
totalsumcredits += credits;
totalsumgradepts += gradepts;

document.write("<tr><td>" + course + "</td><td>" + credits + "</td><td>" + grade + "</td><td>" + gradepts + "</tr> ") ;


}

gpa = Math.round(((totalsumgradepts/totalsumcredits) * 100)/100);

document.write("<tr><td colspan='4'> ;;</td></tr><tr><td colspan='4'><b>Total Grade Points: </b>" + totalsumgradepts +

"</td></tr> <tr><td colspan='4'><b>GPA: </b>" + gpa + "</td></tr></table>");
document.writeln(totalsumcredits);



Back to top Reply with quote
#6   
GoddsEgo
PayPal Donation
CZ Moderator
Status: Offline
Joined: Jun 26, 2003
0.15 posts per day
Posts: 1211
Points: 69,166
 Yahoo Messenger  
Ah I C thats the part that was confusing me. Me only having having a 6 grade diploma given to me by my aunt Flo couldn't understand all that credit stiff. lol

Glad you got it working



Back to top Reply with quote
Display posts from previous:      
Add To: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
<< View previous topic View next topic >>
Post new topicReply to topic

Jump to 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum