...

View Full Version : GPA calculator practice exam



KabkaCaana
03-06-2012, 05:09 AM
I must be misunderstanding a fundamental aspect of javascript because I have been sitting here scratching my head for an hour trying to figure out why my code produces a nAn for the gpa calculation. I intended the calcutator to erase any entries that are not standard grades, and prompt the user to fill in blanks if there are any. I apologize if the code is too long. I think there is a probably a way to cut it down, so if someone could tell me how that would also be extremely helpful.

P.S. the calculator is supposed to convert the letter grade to the gpa score (A-->4.0) so if someone could explain how it would be done in an array instead that would help. I tried to do it, but I couldn't get that to work. This way is functional albeit long.

My main issue is the calculator's inability to calculate!
Thank you for any responses.
I cannot attach anything so I will include a pastebin link instead:
http://pastebin.com/DfyR46p2

jmrker
03-06-2012, 05:56 AM
This is not the final solution to your problem (it's late here and I'm going to bed)
but, it should reduce possible syntax errors in your very lengthly code
if you understand what it is doing.


<script type="text/javascript">

function convertLetterGrade(LetterGrade) {
var grade = '';
switch (LetterGrade) {
case 'A': grade = 4.0; break;
case 'A-': grade = 3.7; break;
case 'B+': grade = 3.5; break;
case 'B': grade = 3.0; break;
case 'B-': grade = 2.7; break;
case 'C+': grade = 2.3; break;
case 'C': grade = 2.0; break;
case 'C-': grade = 1.7; break;
case 'D+': grade = 1.3; break;
case 'D': grade = 1.0; break;
case 'D-': grade = 0.7; break;
case 'F': grade = 0.0; break;
default: alert('Invalid entry'); break;
}
return grade;
}
function calculateGrades() {
var total = 0;
total += convertLetterGrade(grade1);
total += convertLetterGrade(grade2);
total += convertLetterGrade(grade3);
total += convertLetterGrade(grade4);
total += convertLetterGrade(grade5);
if (total != 0) { total = total/5; } else { total = 0; }
return total.toFixed(2)
}
var grade1 = 'A';
var grade2 = 'B';
var grade3 = 'C';
var grade4 = 'B';
var grade5 = 'A';
alert('Average: '+calculateGrades());

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum