Tuesday, 20 May 2014

Today has been spent trying to learn about arrays...

I wrote the script below to try to understand how the Array function and the push function to assemble different arrays. 


<script>

  var array1 = [1,2,3,4,5];
  var alphaarray = ["a","b","c","d","e"];
  var arrayarray = [];
  var pusharray = [];
  var anotherpusharray = [];  
    
    
    for (i = 0; i < array1.length; ++i)
            {   
                arrayarray[i] =Array(array1[i], alphaarray[i]);
                pusharray.push(array1[i],alphaarray[i]);
                
            }
    
    anotherpusharray.push(array1, alphaarray)
  
     document.write("<br>");     
     document.write("<br>");
     document.write(arrayarray);
     document.write("<br>");     
     document.write("<br>");
     document.write(arrayarray[0]);
     // output put is 1,a
     
     

     document.write("<br>");     
     document.write("<br>");
     document.write(pusharray);
     
     document.write("<br>");     
     document.write("<br>");
     document.write(pusharray[0]);
     
  
  // output looks the same for both arrayarray and pusharray even though the information is differently organised.  
  // the different organisation is demonstrated by what happens when you write the first element from each array. The first element from 
  // arrayarray is [1,a]
  
   
  
      document.write("<br>");     
     document.write("<br>");
     document.write(anotherpusharray);
     document.write("<br>");     
     document.write("<br>");
     document.write(anotherpusharray[0]);
     document.write("<br>");     
     document.write("<br>");
     document.write(anotherpusharray[0][0]);
      
 // 
  
    

    </script>

No comments:

Post a Comment