JavaScript
  Home arrow JavaScript arrow Page 3 - JavaScript Arrays
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript Arrays
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 87
    2005-05-25

    Table of Contents:
  • JavaScript Arrays
  • Multidimensional Arrays and Use
  • Beginning Code
  • Associating Images and Links
  • What to Put into the HTML

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    JavaScript Arrays - Beginning Code


    (Page 3 of 5 )

    Begin by adding the following block of code to your text editor:

     deals1 = new Array("images/gcards/gaintek.gif","images/gcards/gfarce.gif",
    "images/gcards/leadward.gif","images/gcards/matrax.gif",
    "images/gcards/nvideon.gif")

    deals2 = new Array("images/mobos/abyte.gif","images/mobos/dfrock.gif",
    "images/mobos/epoch.gif","images/mobos/gigasus.gif",
    "images/mobos/saltech.gif")

    deals3 = new Array("images/hdds/datastore.gif","images/hdds/maxgate.gif",
    "images/hdds/sambell.gif","images/hdds/toshachi.gif",
    "images/hdds/wmax.gif")

    deals4 = new Array("images/cpus/amg.gif",
    "images/cpus/amg32.gif","images/cpus/amg65.gif",
    "images/cpus/nuetrino.gif","images/cpus/outel.gif")

    deals5 = new Array("images/ram/corstan.gif","images/ram/critical.gif",
    "images/ram/generic.gif","images/ram/kingmos.gif",
    "images/ram/twinsung.gif")

    As you can see, there are five arrays, each containing the initial pictures that represent each of the special offers in each of the product lines. Each array is initialized in the format nameof array = new Array with the items in the array comma delimited and contained within brackets and quotes. Each of these statements must be on a single line in the .js file.

    In order to make a different picture from each of the product lines display each time the page is loaded, we need to make use of the length property of the array objects by creating an image counter:

    imgCT1 = deals1.length
    imgCT2 = deals2.length
    imgCT3 = deals3.length
    imgCT4 = deals4.length
    imgCT5 = deals5.length

    The values of each of these variables will clearly be five, but it is necessary so that if you want to add to the number of values in one of the arrays, the script will still include all of the pictures. To handle the randomization of the images, we now need to make use of another of JavaScript’s built in objects, namely the math object:

    function randomize() {
     if (document.images) {
       randomNum1 = Math.floor ((Math.random() * imgCT1))
       randomNum2 = Math.floor ((Math.random() * imgCT2)) 
       randomNum3 = Math.floor ((Math.random() * imgCT3)) 
       randomNum4 = Math.floor ((Math.random() * imgCT4)) 
       randomNum5 = Math.floor ((Math.random() * imgCT5)) 
       document.deal1.src = deals1[randomNum1] 
       document.deal2.src = deals2[randomNum2] 
       document.deal3.src = deals3[randomNum3] 
       document.deal4.src = deals4[randomNum4] 
       document.deal5.src = deals5[randomNum5] 
     }
    }

    This creates a function called randomize that gets called when the page referencing the script loads. It first checks that the browser understands images using the if statement, and provided it does, creates five variables, each containing a random number that is generated using the ((Math.random() * imgCT1)) syntax. The .random property of the math object generates a number between nothing and one, which we then multiply by the relevant imgCT value. The .floor property ensues that the value obtained using the random number is a whole number by rounding the value down to the next whole number. So, if the random number generated is 0.3, you multiply this by 5 (the value of imgCT1) and the random number will be 1.5. We can’t access the index 1.5 from our array because it doesn’t exist, so the floor method rounds this down to 1. The function then sets the src element of the image holder onto the Web page (which we’ll come to in a minute) called deal1 with one of the array indices.

    More JavaScript Articles
    More By Dan Wellman


       · ...you may want to rename mySum to myProduct, since that's the value being...
       · Iteration over indexed and associative arrays would've been a big help.
       · Arrays are zero-indexed. myArray[1] is 89, not 15.Just like every other language...
       · Is not a multidimensional array, but an array of records, or structs (in c#).For...
       · document.write(myArray[1])would write 89 not 15Arrays in JavaScript are zero...
     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway