Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •