Web Analytics Made Easy -
StatCounter multidim arrays failing - CodingForum

Announcement

Collapse
No announcement yet.

multidim arrays failing

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • multidim arrays failing

    Hi folks,

    Got an error with my two-dim array. Says that lstOpts.1 is null. but surely it isn't?!

    Here's my code:
    ----------------------------------------

    <script language="Javascript">

    var lstOpts = new Array(5) ;

    for (i=0; i <5; i++)
    {
    lstOpts[i] = new Array(2) ;

    lstOpts[0][0] = "Computers" ;
    lstOpts[0][1] = "Hardware|Software|Other" ;
    lstOpts[0][2] = "null,Approved Software|New Software,null" ;
    lstOpts[0][3] = "Windows XP|Office XP|Office XP Professional|Word XP|Excel XP|Powerpoint XP|Project XP|Frontpage|Adobe Photoshop|Norton Utilities,null" ;

    lstOpts[1][0] = "Security" ;
    lstOpts[1][1] = "Network|Personal|Building Access" ;
    lstOpts[1][2] = "Email|Internet|Intranet|Database,null,null" ;
    lstOpts[1][3] = "null,null,null,Oracle|SQL|DB2|Sybase" ;

    lstOpts[2][0] = "Telecommunications" ;
    lstOpts[2][1] = "Cellphone|Desk Phone|Pager|Video Conference|Fax machines" ;
    lstOpts[2][2] = "Nokia|Ericson|Motorola,null,null,null,null" ;
    lstOpts[3][3] = "Nokia 8890|Nokia8910|Nokia 8850|Nokia 7650|Nokia 8210,null,null" ;

    lstOpts[3][0] = "Facilities" ;
    lstOpts[3][1] = "Meeting Room|Car Parking|Catering|Equipment Moves" ;
    lstOpts[3][2] = "null,null,null,Single User|Multiple Users|Complete Office" ;
    lstOpts[3][3] = "null,IT Equipment|Office Furniture|Staff & Equipment,null" ;

    lstOpts[4][0] = "Other" ;
    lstOpts[4][1] = "Photo Copiers|New Hires|Imaging Equipment" ;
    lstOpts[4][2] = "null,Technical Staff|Management|Clerical Staff,null";
    lstOpts[4][3] = "null,Executive|Director|Sales|HR,null" ;
    }

    </script>
    ------------------------------------------

    any1 got an idea wot I'm doing wrong?

    thanks.
    *keep it simple (TM)

  • #2
    since you're working with explicit subscripts you should not be setting the values inside a loop. But, after looking at this:
    Code:
    lstOpts[i] = new Array(2) ;
    I'm guessing you meant to close the loop immediately after this statement.

    Even though you are only creating two elements for the second dimention, Javascript dynamically adjusts the size of the array when you reference beyond, in this case, 2. So it's OK but it would be better to declare it as 4 in the first place. Just makes the intent clearer and debugging easier.

    Since you create the first array size explicitly, use the array length attribute in the for loop.

    Is that a digit one or the letter el in "lstOpts"? Don't be afraid to type. "firstOpts" would be better. EVEN better would be a variable name telling me what these "firstOpts" are.

    Finally, good coding practice says "no magic numbers." That is, avoid using explicit numbers; and by using *good* variable names you better document the code. So here's how the new code should look:
    Code:
    var firstDim= 5, secondDim = 4;  // sorely in need of better names here!
    
    var firstOpts = new Array(firstDim) ;
    
    
    for (i=0; i = firstOpts.length; i++) {
       firstOpts[i] = new Array(secondDim) ;
    [b]}[/b]
    
    // ... set array values outside the loop here ...

    Comment

    Working...
    X