Javascript用户输入平均GPA计算器

时间:2015-06-30 06:22:41

标签: javascript

我正在尝试使用javascript开发用户输入gpa计算器。逻辑是,用户只能输入A,B,C,D,F值。然后它将弹出计算平均结果。如果结果3.50或更高,则以祝贺方式弹出平均结果,对不起平均结果。

我试图从2天开始解决它。但没有运气。你能给我任何解决方法吗? 如果我输入值,则显示NaN,但如果我保持为空,则显示0。

JSFiddle:https://jsfiddle.net/jL8p1g80/

<!DOCTYPE html>
    <html>
    <head>
        <title>GPA Calculation </title>
    </head>
    <body>


    <!-- Calculator html design start from here --> 


        <hr>
        <br>
        <h2>GPA Calculation</h2>
        <h3>Here use GPA Scale: A = 4; B = 3; C = 2; D = 1; F = 0</h3>

        <br>
    <CENTER>

    <!-- This is html form -->  
    <FORM Name="GPACalcForm">
    <TABLE BORDER=5 BGCOLOR=#C0C0C0 CELLPADDING="5" 
    CELLSPACING="2">
    <TH></TH>
    <TH>Grade</TH>

    <TR>
    <TD>Class 1</TD>
    <TD><INPUT TYPE=TEXT SIZE=5 NAME="GR1" ALIGN=TOP 
    MAXLENGTH=5></TD>

    </TR>
    <TR>
    <TD>Class 2</TD>
    <TD><INPUT TYPE=TEXT SIZE=5 NAME="GR2" ALIGN=TOP 
    MAXLENGTH=5></TD>

    </TR>
    <TR>
    <TD>Class 3</TD>
    <TD><INPUT TYPE=TEXT SIZE=5 NAME="GR3" ALIGN=TOP 
    MAXLENGTH=5></TD>

    </TR>
    <TR>
    <TD>Class 4</TD>
    <TD><INPUT TYPE=TEXT SIZE=5 NAME="GR4" ALIGN=TOP 
    MAXLENGTH=5></TD>
    </TR>



    <TR ALIGN=CENTER>
    <TD COLSPAN=3><INPUT TYPE="BUTTON" VALUE="Calculate" 
    NAME="CalcButton"
    OnClick="gpacalc()"></TD>
    </TR>
    </TABLE>
    </FORM>
    <BR>
    <P>

    <P>
    </CENTER>

    <BR>
<script type="text/javascript">

function gpacalc()
{
//define valid grades and their values
var gr = new Array(5); 
var cr = new Array(5);
var ingr = new Array(3);


// define valid grades and their values


var grcount = 5; 
gr[0] = "A";
cr[0] = 4;
gr[1] = "B";
cr[1] = 3;
gr[2] = "C";
cr[2] = 2;
gr[3] = "D";
cr[3] = 1;
gr[4] = "F";
cr[4] = 0;



// retrieve user input
ingr[0] = document.GPACalcForm.GR1.value;
ingr[1] = document.GPACalcForm.GR2.value;
ingr[2] = document.GPACalcForm.GR3.value;
ingr[3] = document.GPACalcForm.GR4.value;

// Calculate GPA
var allgr =0;
var gpa = 0;


for (var x = 0; x < 4; x++)
        {
        if (ingr[x] == "") break;  // if empty value then stop
        var validgrcheck = 0;
        for (var xx = 0; xx < grcount; xx++)
                {
                if (ingr[x] == gr[xx])
                        {
                        allgr = allgr + (parseInt(ingr[x],10)); 

                        validgrcheck = 1;
                        break;
                        }
                }
        if (validgrcheck == 0)
                {
                alert("Error- Please use standard college grades in the form of A B C D F.");
                return 0;
                }
        }

gpa = allgr;

if(eval(gpa) >=3.50) {

alert("CONGRATULATION! your GPA =  " + eval(gpa)); 

return 0;
    }
    else {
        alert("Thanks for try your GPA =  " + eval(gpa));
        return 0;
    }
}


</SCRIPT>


</div>

    <div class="footer">

    </div>
</body>
</html> 

1 个答案:

答案 0 :(得分:1)

您的问题就在这一行(parseInt(ingr[x],10))。在这里,您尝试将一个字符(&#39; A&#39; B&#39;等)解析为整数,结果为NaN。而是尝试

allgr = allgr + (cr[x],10);

计算GPA。

顺便说一句,我没有看到计算平均值的逻辑。