Advertisement
RokiAdhytama

Init Array - Chapter 11

Jul 3rd, 2022
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html xmlns = "http://www.w3.org/1999/xhtml">
  2.    <head>
  3.       <title>Initializing an Array</title>
  4.  
  5.       <script type = "text/javascript">
  6.          <!--
  7.          // this function is called when the <body> element's
  8.          // onload event occurs
  9.          function initializeArrays()
  10.          {
  11.             var n1 = new Array( 5 );   // allocate 5-element Array
  12.             var n2 = new Array();      // allocate empty Array
  13.                    
  14.             // assign values to each element of Array n1  
  15.             for ( var i = 0; i < n1.length; ++i )    
  16.                n1[ i ] = i;
  17.                                                
  18.             // create and initialize five-elements in Array n2
  19.             for ( i = 0; i < 5; ++i )
  20.                n2[ i ] = i;
  21.    
  22.             outputArray( "Array n1 contains", n1 );
  23.             outputArray( "Array n2 contains", n2 );
  24.          }
  25.  
  26.          // output "header" followed by a two-column table
  27.          // containing subscripts and elements of "theArray"  
  28.          function outputArray( header, theArray )
  29.          {
  30.             document.writeln( "<h2>" + header + "</h2>" );
  31.             document.writeln( "<table border = \"1\" width =" +
  32.                "\"100%\">" );
  33.    
  34.             document.writeln( "<thead><th width = \"100\"" +
  35.                "align = \"left\">Subscript</th>" +
  36.                "<th align = \"left\">Value</th></thead><tbody>" );
  37.    
  38.             for ( var i = 0; i < theArray.length; i++ )
  39.                document.writeln( "<tr><td>" + i + "</td><td>" +
  40.                   theArray[ i ] + "</td></tr>" );
  41.  
  42.             document.writeln( "</tbody></table>" );
  43.          }
  44.          // -->
  45.       </script>
  46.  
  47.    </head><body onload = "initializeArrays()"></body>
  48. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement