Forum Moderators: open
here is the full code:
<html>
<head>
<title>Calculate your GPA</title>
<script type = "text/javascript">
//function to set the focus on GPA form
function setFocus() {
document.GPA.focus();
return;
}
//functio to round 2 decimal points
function round(n){return(.01* Math.round(100*n));}
//function to calculate gpa
function calculate(fr)
{
//checking if all credit hours equal to zero
if (fr.CH1.selectedIndex=="0" &&
fr.CH2.selectedIndex=="0" && fr.CH3.selectedIndex=="0" &&
fr.CH4.selectedIndex=="0" && fr.CH5.selectedIndex=="0" && fr.CH6.selectedIndex=="0" )
{alert ("Please specify your credit hours") }
else
//calculating
var resultgpa;
{fr.resultgpa.value
=round((eval(fr.CH1.selectedIndex)*parseFloat(fr.grade1.value)+
eval(fr.CH2.selectedIndex)*parseFloat(fr.grade2.value)+
eval(fr.CH3.selectedIndex)*parseFloat(fr.grade3.value)+
eval(fr.CH4.selectedIndex)*parseFloat(fr.grade4.value)+
eval(fr.CH5.selectedIndex)*parseFloat(fr.grade5.value)+
eval(fr.CH6.selectedIndex)*parseFloat(fr.grade6.value))
/(eval(fr.CH1.selectedIndex)+
eval(fr.CH2.selectedIndex)+eval(fr.CH3.selectedIndex)+
eval(fr.CH4.selectedIndex)+eval(fr.CH5.selectedIndex)+eval(fr.CH6.selectedIndex)))
var resultcgpa;
fr.resultcgpa.value
=round((eval(fr.CH1.selectedIndex)*parseFloat(fr.grade1.value)+
eval(fr.CH2.selectedIndex)*parseFloat(fr.grade2.value)+
eval(fr.CH3.selectedIndex)*parseFloat(fr.grade3.value)+
eval(fr.CH4.selectedIndex)*parseFloat(fr.grade4.value)+
eval(fr.CH5.selectedIndex)*parseFloat(fr.grade5.value)+
eval(fr.CH6.selectedIndex)*parseFloat(fr.grade6.value)+
parseFloat(fr.CGPA.value)*parseFloat(fr.TCH.value))
/(eval(fr.CH1.selectedIndex)+
eval(fr.CH2.selectedIndex)+eval(fr.CH3.selectedIndex)+
eval(fr.CH4.selectedIndex)+eval(fr.CH5.selectedIndex)+
eval(fr.CH6.selectedIndex)+parseFloat(fr.TCH.value)))
}
}
</script>
</head>
<body>
<!--creating table to create 6 row of drop down lists to choose-->
<table border="0" width="400" align="center">
<tr>
<td width="400" bgcolor="#999999">
<p align="center"><font color="white"><span style="font-size:12pt;"><b>Calculate your GPA & CGPA</b></span></font></p>
</td>
</tr>
</table>
<p>
<table width="400" border="0" align="center" valign="top">
<!--creating form to send data to function-->
<form action="none" name="GPA">
<tr>
<th>Subject</th>
<th>Credits</th>
<th>Grade</th>
</tr>
<tr>
<td align = "center" width = "165">1. <input type = text name="sn1"></td>
<td align = "center">
<select name= "CH1">
<option value = "0" SELECTED>Select
<option value = "1">1
<option value = "2">2
<option value = "3">3
<option value = "4">4
</select></td>
<td align = "center" width = "70">
<select name= "grade1">
<option value = "5" SELECTED>Select
<option value = "4">A
<option value = "3.67">A-
<option value = "3.33">B+
<option value = "3">B
<option value = "2.67">B-
<option value = "2.33">C+
<option value = "2">C
<option value = "1.67">C-
<option value = "1.33">D+
<option value = "1">D
<option value = "0">E
</select></td></tr>
<tr><td align = "center">2. <input type = text name="sn2"></td>
<td align = "center">
<select name= "CH2">
<option value = "0" SELECTED>Select
<option value = "1">1
<option value = "2">2
<option value = "3">3
<option value = "4">4
</select></td>
<td align = "center">
<select name= "grade2">
<option value = "5" SELECTED>Select
<option value = "4">A
<option value = "3.67">A-
<option value = "3.33">B+
<option value = "3">B
<option value = "2.67">B-
<option value = "2.33">C+
<option value = "2">C
<option value = "1.67">C-
<option value = "1.33">D+
<option value = "1">D
<option value = "0">E
</select></td></tr>
<tr><td align = "center">3. <input type = text name="sn3"></td>
<td align = "center">
<select name= "CH3">
<option value = "0" SELECTED>Select
<option value = "1">1
<option value = "2">2
<option value = "3">3
<option value = "4">4
</select></td>
<td align = "center">
<select name= "grade3">
<option value = "5" SELECTED>Select
<option value = "4">A
<option value = "3.67">A-
<option value = "3.33">B+
<option value = "3">B
<option value = "2.67">B-
<option value = "2.33">C+
<option value = "2">C
<option value = "1.67">C-
<option value = "1.33">D+
<option value = "1">D
<option value = "0">E
</select></td></tr>
<tr><td align = "center">4. <input type = text name="sn4"></td>
<td align = "center">
<select name= "CH4">
<option value = "0" SELECTED>Select
<option value = "1">1
<option value = "2">2
<option value = "3">3
<option value = "4">4
</select></td>
<td align = "center">
<select name= "grade4">
<option value = "5" SELECTED>Select
<option value = "4">A
<option value = "3.67">A-
<option value = "3.33">B+
<option value = "3">B
<option value = "2.67">B-
<option value = "2.33">C+
<option value = "2">C
<option value = "1.67">C-
<option value = "1.33">D+
<option value = "1">D
<option value = "0">E
</select></td></tr>
<tr><td align = "center">5. <input type = text name="sn5"></td>
<td align = "center">
<select name= "CH5">
<option value = "0" SELECTED>Select
<option value = "1">1
<option value = "2">2
<option value = "3">3
<option value = "4">4
</select></td>
<td align = "center">
<select name= "grade5">
<option value = "5" SELECTED>Select
<option value = "4">A
<option value = "3.67">A-
<option value = "3.33">B+
<option value = "3">B
<option value = "2.67">B-
<option value = "2.33">C+
<option value = "2">C
<option value = "1.67">C-
<option value = "1.33">D+
<option value = "1">D
<option value = "0">E
</select></td></tr>
<tr><td align = "center">6. <input type = text name="sn6"></td>
<td align = "center">
<select name= "CH6">
<option value = "0" SELECTED>Select
<option value = "1">1
<option value = "2">2
<option value = "3">3
<option value = "4">4
</select></td>
<td align = "center">
<select name= "grade6">
<option value = "5" SELECTED>Select
<option value = "4">A
<option value = "3.67">A-
<option value = "3.33">B+
<option value = "3">B
<option value = "2.67">B-
<option value = "2.33">C+
<option value = "2">C
<option value = "1.67">C-
<option value = "1.33">D+
<option value = "1">D
<option value = "0">E
</select>
</td></tr>
</table>
<table width="400" border="0" align="center" valign="top">
<tr><hr width = "400"></tr>
<tr>
<td>
Your current CGPA:
</td><td align="right"><input type = text name="CGPA"></td></tr>
<tr><td>Your credit hours taken is: </td><td align="right"><input type = text name="TCH"></td></tr>
</table>
<table width="400" border="0" align="center" valign="top">
<tr><hr width = "400"></tr>
<tr>
<td>
<!--sending information to calculate() function-->
<!--getting back result from function-->
<tr><td>Your GPA will be:</td>
<td align="right"><input type = text name="resultgpa"></td></tr>
<!--getting back result from function-->
<tr><td>Your CGPA will be:</td>
<td align="right"><input type = text name="resultcgpa"></td></tr>
<tr><td><input type = "button" value = "Calculate!" onclick = "calculate(this.form)" name = "bottun"></td>
<!--reset button-->
<td align="right"><input type="reset" name="reset" value="Clear">
<!--print button-->
<input type="button" value="Print" onClick="javascript: window.print()" name="button2"></td></tr>
</table></form>
<table border="0" width="400" align="center">
<tr>
<td width="400" bgcolor="#999999">
<p align="center"><font color="white"><span style="font-size:12pt;"><b>Good Luck!</b></span></font></p>
</td>
</tr>
</table>
</body>
</html>
So, the problem must be with the input. Without chugging through all the code as it's quite long, is there a specific combination in your form that produces the error?
Could it be that the answer is a whole number, so there are no decimal points? In this case maybe it's simply a case of formatting the display?
ok, after looking at the code, it's a bit, well, unnecessary. Let's rewrite your calculate function to make it a little more readable.
All those evals and parsefloats? You're only evaluating and parsing your own option values. No need. Also, your select values shouldn't be coded like that, it's not 100% reliable cross browser, the correct way is to use their selectedIndex value. Any one of those things could be causing an inconsistent error.
The only input we need to check is your TCH value. Parsefloat won't do it, as it won't throw an error if the field contains invalid characters, so a regex will be fine.
So, here goes (remember to replace the ¦¦ characters!).....
//function to calculate gpa
function calculate(fr)
{
// Shorthand for selected indexes
var idx1 = fr.CH1.selectedIndex;
var idx2 = fr.CH2.selectedIndex;
var idx3 = fr.CH3.selectedIndex;
var idx4 = fr.CH4.selectedIndex;
var idx5 = fr.CH5.selectedIndex;
var idx6 = fr.CH6.selectedIndex;
//checking if all credit hours equal to zero
if ( !( idx1 + idx2 + idx3 + idx4 + idx5 + idx6)) {
alert ("Please specify your credit hours");
return;
}
// Shorthand for grade values
var gr1 = idx1 * fr.grade1.option[fr.grade1.selectedIndex].value;
var gr2 = idx2 * fr.grade2.option[fr.grade2.selectedIndex].value;
var gr3 = idx3 * fr.grade3.option[fr.grade3.selectedIndex].value;
var gr4 = idx4 * fr.grade4.option[fr.grade4.selectedIndex].value;
var gr5 = idx5 * fr.grade5.option[fr.grade5.selectedIndex].value;
var gr6 = idx6 * fr.grade6.option[fr.grade6.selectedIndex].value;
// Check CGPA and TCH field
var cgpa = fr.CGPA.value;
var tch = fr.TCH.value;
if( isNaN( cgpa) ¦¦ isNaN( tch)) {
alert( "Please enter valid numbers for CGPA and credit hours taken");
return;
}
//calculating
var gradeSum = gr1 + gr2 + gr3 + gr4 + gr5 + gr6;
var idxSum = idx1 + idx2 + idx3 + idx4 + idx5 + idx6;
fr.resultgpa.value = round( gradeSum / idxSum);
fr.resultcgpa.value = round( (gradeSum +( cgpa * tch)) / (idxSum + tch));
}
code not tested so please forgive any typos!