Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-06-2012, 04:09 AM   PM User | #1
KabkaCaana
New to the CF scene

 
Join Date: Mar 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
KabkaCaana is an unknown quantity at this point
GPA calculator practice exam

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
KabkaCaana is offline   Reply With Quote
Old 03-06-2012, 04:56 AM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

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.
Code:
<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>
jmrker is offline   Reply With Quote
Reply

Bookmarks

Tags
alert, arrays, calculator, gpa, parallel

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:55 PM.


Advertisement
Log in to turn off these ads.