Letter grade value to number value then averaged

时间:2015-07-31 20:07:51

标签: javascript string math

I'm writing a script for my schools web form for calculating credits and gpa values. When I run the input for letter grades I convert to uppercase and then check with if/else statements for A-F options and convert the value of the input to a number value which is then passed to another function to average the total for the column inputs which is then returned back to a letter grade. The issue i'm having is that when i use my keyup jquery function it returns a NaN value instead of a letter... HELP

function CalcGPA(a,b,c,d,e,f,g,h,i,j){
var initial=a+b+c+d+e+f+g+h+i+j;
var total=initial/10;
return total;}

function Convert(a){
var b=a.value.toUpperCase();
if(b.value="A")
{
    b=4.0;
    return b;
}
else if(b.value="A-")
{
    b=3.67;
    return b;
}
else if(b.value="B+")
{
    b=3.33;
    return b;
}
else if(b.value="B")
{
    b=3.0;
    return b;
}
else if(b.value="B-")
{
    b=2.67;
    return b;
}
else if(b.value="C+")
{
    b=2.33;
    return b;
}
else if(b.value="C"){
    b=2.0;
    return b;
}
else if(b.value="C-")
{
    b=1.7;
    return b;
}
else if(b.value="D")
{
    b=1.0;
    return b;
}
else {
b=0.0;}
return b;}

function toLetter(a){
if(a<=4||a>=3.68)
{
    a="A";
}
else if(a<=3.67 || a>=3.34)
{
    a="A-";
}
else if(a<=3.33 || a>=3.1)
{
    a="B+";
}
else if(a<=3.0 || a>=2.68)
{
    a="B";
}
else if(a<=2.67 || a>=2.34)
{
    a="B-";
}
else if(a<=2.33 || a>=2.1)
{
    a="C+";
}
else if(a<=2.0 || a>=1.8)
{
    a="C";
}
else if(a<=1.7 || a>=1.4)
{
a="C-";
}
else if(a<=1.3 || a>=1.1)
{
    a="D+";
}
else if (a=1.0)
{
    a="D";
}
else {
a="F";}
return a;}

3 个答案:

答案 0 :(得分:3)

You should use parseInt or parseFloat in your toLetter function I suppose, and in your if statements, use two equal signs instead of one in order to compare:

function toLetter(a) {
    var a = parseInt(a); //or parseFloat if you need decimals.
    //rest of your code...

I strongly recommend a little refactoring here, for instance, instead of declaring and then returning, just return:

else if(b.value == "B+")
{
    return 3.33;
}

Another improvement, move your statements to a switch:

switch(b.value) {
    case "B+" :
        return 3.33;
    case "B" :
        return 3.0;
}

And, one thing I would do, instead of conditions or cases, use a map:

var scoresForValues = {
    "A": 4.0,
    "A-": 3.67,
    "B+": 3.33 //Keep filling until you get all your values.
};

And then your Convert function would be really simple and short:

function Convert(a) {
    var b = a.value.toUpperCase();
    return scoresForValues[b];
}

答案 1 :(得分:0)

Use parseInt for integers or if you want to use decimal values, use parseFloat.

答案 2 :(得分:0)

There are a couple of things here:

First, you are trying to compare using "=", and that can't work. You need to use "==". For example, else if(b.value="C+") should be else if(b.value=="C+")

Second, you've made b a string when you declared it and assigned the input to it: var b=a.value.toUpperCase();

So, don't assign the numerical value to it and return it, just return the numerical value. Same for your a variable in toLetter. Just return the letter value and you can bail out of all your elseif lines as soon as you have a match.

So, instead of a="B-";, just return "B-"

finally, you should probably use switch case instead of your pile of if... else if. NOTE: you don't need break; after return 3 (for instance) because a return bails you out of the function. If you're using case switch for anything not causing a return, leaving out break; will cause you a lot of hurt.

function Convert(a){
var b=a.value.toUpperCase();
switch (b) {
  case "A":
    return 4.0;
    break;
  case "A-":
    return 3.67;
    break;  
  case "B+":
    return 3.33;
    break;
  case "B":
    return 3;
    break;
  case "B-":
    return 2.67;
    break;
  case "C+":
    return 2.33;
    break;
  case "C":
    return 2;
    break;
  case "C-":
    return 1.67;
    break;
  case "D":
    return 1;
    break;
  default:
    return 0;
}