Page 1 of 1

JavaScript help!

PostPosted: Sun Apr 10, 2005 8:30 pm
by Rogie
*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:

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!

PostPosted: Mon Apr 11, 2005 9:40 am
by Mithrandir
It thinks you are using text. Add "zero" to the grades to force it into numeric context:
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() {
 Grades[0] += 0;
 Grades[1] += 0;
 Grades[2] += 0;
 Grades[3] += 0;
 Grades[4] += 0;
 Grades[5] += 0;
 Grades[6] += 0;
 Grades[7] += 0;
 Grades[8] += 0;
 Grades[9] += 0;
 Grades[10] += 0;
 Grades[11] += 0;
 Grades[12] += 0;
 
 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>

PostPosted: Mon Apr 11, 2005 10:07 am
by Rogie
Thanks, oldphil, but that didn't work (although it made sense when I read it). When it passes the answer to the last text box, it gives me a crazy response. If I'm averaging all 100's, for example, I should get an average of 100, but instead I get 7.693077000007693e+49.

But I do know how to put code into my responses on here now! [code] tags! :thumb: Any other ideas?

PostPosted: Mon Apr 11, 2005 9:36 pm
by Rogie
Okay, never mind, I figured it out. You were right, oldphil, but adding 0 didn't work. It merely appended a "0" string to the current string. Instead, I multiplied each array value by 1.0, since text cannot be multiplied in JS, which converted each value to a float.

Thanks for your help, oldphil, because without your input, I wouldn't have figured it out! :thumb:

PostPosted: Fri Apr 15, 2005 7:39 am
by Mithrandir
Did you by any chance add "0" instead of 0? That would do what you said. When I tested the page I posted, it worked fine on my computer. Anyway, glad you got it figured out!

PostPosted: Fri Apr 15, 2005 8:22 am
by Rogie
No, I just copied what you put and inserted it into my code and tried it out. It seems like everyone in my class was having the same problem, and everyone had to either multiply or divide by 1 to get it.

Weird how it worked on yours, though, and not on mine. Oh, well. Thanks anyway!

PostPosted: Fri Apr 15, 2005 8:41 am
by Mithrandir
Do me a favor. Try SUBTRACTING from the number and see if that works. The plus sign appears to have been overloaded to use in alpha context at some point when I wasn't looking (only micro$oft could wield that kind of annoying, pointless power).

I'm just curious if that opperator has been modded as well. It seems to me that it SHOULDN'T have been, but you never know.

PostPosted: Fri Apr 15, 2005 10:19 am
by skynes
just wondering, you have a line that says
Grades = new Array(13);
Should it not be
Grades = new Array[13]; ?

Ive never done Java-script, but Java is my primary languages and I know in Java when using arrays its [] brackets to use it.

PostPosted: Fri Apr 15, 2005 1:11 pm
by Mithrandir
O.o