Advertisement
RokiAdhytama

Pass Array - Chapter 11

Jul 3rd, 2022
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. <html xmlns = "http://www.w3.org/1999/xhtml">
  2. <head>
  3. <title>Passing Arrays and Individual Array
  4. Elements to Functions</title>
  5.  
  6. <script type = "text/javascript">
  7. <!--
  8. function start()
  9. {
  10. var a = [ 1, 2, 3, 4, 5 ];
  11.  
  12. document.writeln( "<h2>Effects of passing entire " +
  13. "array by reference</h2>" );
  14. outputArray(
  15. "The values of the original array are: ", a );
  16.  
  17. modifyArray( a ); // array a passed by reference
  18.  
  19. outputArray(
  20. "The values of the modified array are: ", a );
  21.  
  22. document.writeln( "<h2>Effects of passing array " +
  23. "element by value</h2>" +
  24. "a[3] before modifyElement: " + a[ 3 ] );
  25.  
  26. modifyElement( a[ 3 ] );
  27.  
  28. document.writeln(
  29. "<br />a[3] after modifyElement: " + a[ 3 ] );
  30. }
  31.  
  32. // outputs "header" followed by the contents of "theArray"
  33. function outputArray( header, theArray )
  34. {
  35. document.writeln(
  36. header + theArray.join( " " ) + "<br />" );
  37. }
  38.  
  39. // function that modifies the elements of an array
  40. function modifyArray( theArray )
  41. {
  42. for ( var j in theArray )
  43. theArray[ j ] *= 2;
  44. }
  45.  
  46. // function that attempts to modify the value passed
  47. function modifyElement( e )
  48. {
  49. e *= 2;
  50. document.writeln( "<br />value in modifyElement: " + e );
  51. }
  52. // -->
  53. </script>
  54.  
  55. </head><body onload = "start()"></body>
  56. </html>
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement