JavaScript help!

Homework giving you a headache? Math gives you a migraine? Can't quite figure out how to do something in photoshop? Never fear, the other members of CAA share their expertise in this forum.

JavaScript help!

Postby Rogie » 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:

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!
Zar wrote:Praise God for all things awesome. Life ROCKS!!!!!!!!!!!!!!!!!!!!!!

But sanctify the Lord your God in your hearts: and be ready always to give an answer to every man that asketh you a reason of the hope that is in you with meekness and fear:
-- 1 Peter 3:15
User avatar
Rogie
 
Posts: 2975
Joined: Thu Oct 09, 2003 12:00 pm
Location: Virginia

Postby Mithrandir » Mon Apr 11, 2005 9:40 am

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>
User avatar
Mithrandir
 
Posts: 11071
Joined: Fri Jun 27, 2003 12:00 pm
Location: You will be baked. And then there will be cake.

Postby Rogie » Mon Apr 11, 2005 10:07 am

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?
Zar wrote:Praise God for all things awesome. Life ROCKS!!!!!!!!!!!!!!!!!!!!!!

But sanctify the Lord your God in your hearts: and be ready always to give an answer to every man that asketh you a reason of the hope that is in you with meekness and fear:
-- 1 Peter 3:15
User avatar
Rogie
 
Posts: 2975
Joined: Thu Oct 09, 2003 12:00 pm
Location: Virginia

Postby Rogie » Mon Apr 11, 2005 9:36 pm

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:
Zar wrote:Praise God for all things awesome. Life ROCKS!!!!!!!!!!!!!!!!!!!!!!

But sanctify the Lord your God in your hearts: and be ready always to give an answer to every man that asketh you a reason of the hope that is in you with meekness and fear:
-- 1 Peter 3:15
User avatar
Rogie
 
Posts: 2975
Joined: Thu Oct 09, 2003 12:00 pm
Location: Virginia

Postby Mithrandir » Fri Apr 15, 2005 7:39 am

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!
User avatar
Mithrandir
 
Posts: 11071
Joined: Fri Jun 27, 2003 12:00 pm
Location: You will be baked. And then there will be cake.

Postby Rogie » Fri Apr 15, 2005 8:22 am

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!
Zar wrote:Praise God for all things awesome. Life ROCKS!!!!!!!!!!!!!!!!!!!!!!

But sanctify the Lord your God in your hearts: and be ready always to give an answer to every man that asketh you a reason of the hope that is in you with meekness and fear:
-- 1 Peter 3:15
User avatar
Rogie
 
Posts: 2975
Joined: Thu Oct 09, 2003 12:00 pm
Location: Virginia

Postby Mithrandir » Fri Apr 15, 2005 8:41 am

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.
User avatar
Mithrandir
 
Posts: 11071
Joined: Fri Jun 27, 2003 12:00 pm
Location: You will be baked. And then there will be cake.

Postby skynes » Fri Apr 15, 2005 10:19 am

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.
I am the Reaper of Souls... and it's harvest time.

Image
User avatar
skynes
 
Posts: 742
Joined: Mon Mar 01, 2004 8:39 am
Location: N Ireland

Postby Mithrandir » Fri Apr 15, 2005 1:11 pm

O.o
User avatar
Mithrandir
 
Posts: 11071
Joined: Fri Jun 27, 2003 12:00 pm
Location: You will be baked. And then there will be cake.


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 116 guests