gpt4 book ai didi

Javascript 用户输入平均 GPA 计算器

转载 作者:行者123 更新时间:2023-12-03 09:42:59 25 4
gpt4 key购买 nike

我正在尝试在javascript的帮助下开发一个用户输入gpa计算器。逻辑是,用户只能输入A、B、C、D、F值。然后它会弹出平均结果。如果结果为 3.50 或以上,则弹出平均结果并表示祝贺,否则对平均结果表示抱歉。

我花了两天时间试图解决这个问题。但没有运气。你能给我任何提示来解决这个问题吗?如果我输入值,它会显示 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>

最佳答案

您的问题出在这一行(parseInt(ingr[x],10))。在这里,您尝试将字符(“A”、“B”等)解析为整数,结果为 NaN。相反尝试

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

计算 GPA。

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

关于Javascript 用户输入平均 GPA 计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31131020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com