我是JavaScript和HTML的新手。我正在尝试使用Javascript制作GPA计算器并将其添加到我的博客中:http://comptechniqz.blogspot.in/2016/04/gpa-both-sems.html 使用html的布局工作正常,但单击时计算按钮不执行任何操作。你能帮我找一下我在做错的事吗?
由于
我的代码:
<form name="sem1">
<table border=5 bgcolor=#BADA55 cellpadding="5" cellspacing="2">
<th>Subject</th>
<th>Grade</th>
<tr>
<td>
<select name="subCRsem1_1" >
<option value="3.5">UMA003</option>
<option value="4.5">UCB008</option>
<option value="4.5">UEC001</option>
<option value="2.5">UES009</option>
<option value="4.0">UTA007</option>
<option value="3.0">UEN002</option>
<option value="4.5">UPH004</option>
<option value="4.5">UEE001</option>
<option value="3.0">UHU004</option>
<option value="4.0">UTA008</option>
</select>
</td>
<td><input type=text size=5 name="gr1" align=top maxlength=5></td>
</tr>
<tr>
<td>
<select name="subCRsem1_2" >
<option value="3.5">UMA003</option>
<option value="4.5">UCB008</option>
<option value="4.5">UEC001</option>
<option value="2.5">UES009</option>
<option value="4.0">UTA007</option>
<option value="3.0">UEN002</option>
<option value="4.5">UPH004</option>
<option value="4.5">UEE001</option>
<option value="3.0">UHU004</option>
<option value="4.0">UTA008</option>
</select>
</td>
<td><input type=text size=5 name="gr2" align=top maxlength=5></td>
</tr>
<tr>
<td>
<select name="subCRsem1_3" >
<option value="3.5">UMA003</option>
<option value="4.5">UCB008</option>
<option value="4.5">UEC001</option>
<option value="2.5">UES009</option>
<option value="4.0">UTA007</option>
<option value="3.0">UEN002</option>
<option value="4.5">UPH004</option>
<option value="4.5">UEE001</option>
<option value="3.0">UHU004</option>
<option value="4.0">UTA008</option>
</select>
</td>
<td><input type=text size=5 name="gr3" align=top maxlength=5></td>
</tr>
<tr>
<td>
<select name="subCRsem1_4" >
<option value="3.5">UMA003</option>
<option value="4.5">UCB008</option>
<option value="4.5">UEC001</option>
<option value="2.5">UES009</option>
<option value="4.0">UTA007</option>
<option value="3.0">UEN002</option>
<option value="4.5">UPH004</option>
<option value="4.5">UEE001</option>
<option value="3.0">UHU004</option>
<option value="4.0">UTA008</option>
</select>
</td>
<td><input type=text size=5 name="gr4" align=top maxlength=5></td>
</tr>
<tr>
<td>
<select name="subCRsem1_5" >
<option value="3.5">UMA003</option>
<option value="4.5">UCB008</option>
<option value="4.5">UEC001</option>
<option value="2.5">UES009</option>
<option value="4.0">UTA007</option>
<option value="3.0">UEN002</option>
<option value="4.5">UPH004</option>
<option value="4.5">UEE001</option>
<option value="3.0">UHU004</option>
<option value="4.0">UTA008</option>
</select>
</td>
<td><input type=text size=5 name="gr5" align=top maxlength=5></td>
</tr>
<tr>
<td>
<select name="subCRsem1_6" >
<option value="3.5">UMA003</option>
<option value="4.5">UCB008</option>
<option value="4.5">UEC001</option>
<option value="2.5">UES009</option>
<option value="4.0">UTA007</option>
<option value="3.0">UEN002</option>
<option value="4.5">UPH004</option>
<option value="4.5">UEE001</option>
<option value="3.0">UHU004</option>
<option value="4.0">UTA008</option>
</select>
</td>
<td><input type=text size=5 name="gr6" 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>
</center>
<br>
<script language="JavaScript">
<!--
function gpaCalc()
{
//define valid grades and their values
var grade = new array(11);
var credit = new array(11);
var gradeReceived = new array(6);
var creditHour = new array(6);
// define valid grades and their values
grade = ['A+', 'A', 'A-', 'B', 'B-', 'C', 'C-', 'E', 'F', 'I', 'X'];
credit = [10, 10, 9, 8, 7, 6, 5, 2, 0, 0, 0];
// retrieve user input
gradeReceived[0] = document.sem1.gr1.value;
gradeReceived[1] = document.sem1.gr2.value;
gradeReceived[2] = document.sem1.gr3.value;
gradeReceived[3] = document.sem1.gr4.value;
gradeReceived[4] = document.sem1.gr5.value;
gradeReceived[5] = document.sem1.gr6.value;
// Allocate credits per subject
creditHour[0] = document.sem1.subCRsem1_1[document.sem1.subCRsem1_1.selectedIndex].value;
creditHour[1] = document.sem1.subCRsem1_2[document.sem1.subCRsem1_2.selectedIndex].value;
creditHour[2] = document.sem1.subCRsem1_3[document.sem1.subCRsem1_3.selectedIndex].value;
creditHour[3] = document.sem1.subCRsem1_4[document.sem1.subCRsem1_4.selectedIndex].value;
creditHour[4] = document.sem1.subCRsem1_5[document.sem1.subCRsem1_5.selectedIndex].value;
creditHour[5] = document.sem1.subCRsem1_6[document.sem1.subCRsem1_6.selectedIndex].value;
// calculate gpa
var totalGP = 0, GPA, totalHour=0;
var i, j, validGrade=0;
for(i=0; i<6; i++)
{
for(j=0; j<11;j++)
{
if(gradeReceived[i]==grade[j])
{
validGrade=1;
break;
}
}
if(validGrade==1)
{
totalGP+=credit[j]*creditHour[i];
totalHour+=creditHour[i];
}
else
{
alert("Error- Please use A+, A, A-, B, B-, C, C-, E, F, I or X in grades " );
return 0;
}
}
GPA=totalGP/totalHour;
alert("gpa = " + eval(GPA));
return 0;
}
//-->
</script>
答案 0 :(得分:1)
限制您的数组。
function gpaCalc()
{
//define valid grades and their values
var grade = new array(11); <-------- array has Caps in the A. should be Array(11) for all;
var credit = new array(11);
var gradeReceived = new array(6);
var creditHour = new array(6);
[UPDATE]
更改javascript的这一部分
if(validGrade==1)
{
totalGP+=credit[j]*creditHour[i];
totalHour+=creditHour[i];
}
到此:
if(validGrade==1)
{
totalGP+=credit[j]*creditHour[i];
totalHour+=parseFloat(creditHour[i]);
}