Advertisement
RokiAdhytama

Sum Array - Chapter 11

Jul 4th, 2022
591
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>Sum the Elements of an Array</title>
  4.  
  5.       <script type = "text/javascript">
  6.          <!--
  7.          function start()
  8.          {
  9.             var theArray = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
  10.             var total1 = 0, total2 = 0;
  11.  
  12.             for ( var i = 0; i < theArray.length; i++ )
  13.                total1 += theArray[ i ];
  14.      
  15.             document.writeln( "Total using subscripts: " + total1 );
  16.  
  17.             for ( var element in theArray )
  18.                total2 += theArray[ element ];
  19.      
  20.             document.writeln( "<br />Total using for...in: " +
  21.                total2 );
  22.          }
  23.          // -->
  24.       </script>
  25.  
  26.    </head><body onload = "start()"></body>
  27. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement