JavaScript help!
PostPosted: Sun Apr 10, 2005 8:30 pm
*sigh* I have no idea what's going on here. I'm trying to write a JavaScript HTML page that takes 13 grades and averages them, but I cannot get it to work! I've done simple average assignments in other programming languages before, but for some reason, I cannot see what I'm doing wrong in JS here. It has to be something so small that I'll never catch it without someone else looking at it. I'd appreciate the help. It doesn't have to be pretty, the teacher said; it just has to work. Here's the code:
Thanks, guys!
- Code: Select all
<html>
<head>
<title>Grade Average Page</title>
<pre>
<script language="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
Grades = new Array(13);
function assign_value(array_index,grade) {
Grades[array_index] = grade;
}
function average() {
sum = Grades[0] + Grades[1] + Grades[2] + Grades[3] + Grades[4] + Grades[5] + Grades[6] + Grades[7] + Grades[8] + Grades[9] + Grades[10] + Grades[11] + Grades[12];
return sum/13;
}
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</pre>
</head>
<body>
<h1>Average of Students' Grades</h1>
<hr />
<form>
<h3>Enter student grades in each field below:</h3>
<p>
Student 01:  
<input type="text" name="student0" size="5" onChange="assign_value(0,student0.value);" />
Student 02:  
<input type="text" name="student1" size="5" onChange="assign_value(1,student1.value);" />
Student 03:  
<input type="text" name="student2" size="5" onChange="assign_value(2,student2.value);" />
Student 04:  
<input type="text" name="student3" size="5" onChange="assign_value(3,student3.value);" />
Student 05:  
<input type="text" name="student4" size="5" onChange="assign_value(4,student4.value);" />
Student 06:  
<input type="text" name="student5" size="5" onChange="assign_value(5,student5.value);" />
Student 07:  
<input type="text" name="student6" size="5" onChange="assign_value(6,student6.value);" />
Student 08:  
<input type="text" name="student7" size="5" onChange="assign_value(7,student7.value);" />
Student 09:  
<input type="text" name="student8" size="5" onChange="assign_value(8,student8.value);" />
Student 10:  
<input type="text" name="student9" size="5" onChange="assign_value(9,student9.value);" />
Student 11:  
<input type="text" name="student10" size="5" onChange="assign_value(10,student10.value);" />
Student 12:  
<input type="text" name="student11" size="5" onChange="assign_value(11,student11.value);" />
Student 13:  
<input type="text" name="student12" size="5" onChange="assign_value(12,student12.value);" />
<input type="button" value="Average Grades" onClick="average_result.value=average();" />  
<input type="reset" value="Reset" />
Class average:  
<input type="text" name="average_result" size="10" />
</p>
</form>
</body>
</html>
Thanks, guys!