BuiltWithNOF
Array Template

This program will be the template you will use to practice writing code using arrays.  All you will need is to write the function, and you will assume an existing array. This program has an existing example function called findAverage.



<html>
   <head>
     <title>Program with Arrays Template</title>

     <script type = "text/javascript">
         <!--
         var a = new Array( 100 ); // create an Array
         var count = 0;
         var out = "";
 
         function buttonPressed()
         {
             var searchKey = searchForm.inputVal.value;

             if ( searchKey == "" )
                 findAverage();
             else
             {
                 a[count] = parseInt(searchKey);
                 count++;
                 searchForm.inputVal.value = "";
                 searchForm.inputVal.focus();
             }
         }
  
        
         function findAverage( )
         {  
             var sum = 0;
             for ( var n = 0; n < count; ++n )
               sum = sum + a[n];

             searchForm.result.value = sum/count
          
             for ( var x=0; x<count; x++)
               out = out + a[x] + ", ";

             searchForm.arrayout.value = out;
         }
         // -->
     </script>

   </head>

   <body>


     <form name = "searchForm" action = "">
         <p>Enter integer score or nothing to exit<br />
        
         <input name = "inputVal" type = "text" />
         <input name = "search" type = "button" value = "Add"
               onclick = "buttonPressed()" /><br /></p>

         <p>Result<br />
         <input name = "result" type = "text" size = "30" /></p>

         <p>Array Dump<br />
         <input name = "arrayout" type = "text" size = "80" /></p>
     </form>
   </body>
</html>
 
 
See It Run
 

 

[Home] [Syllabus] [Sessions] [Writing Code] [Help] [Setup] [Instructor]