Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do i create a 2d array with loops?

    If i wanted to basically create a 2d array with 2 loops, how would i do it? I thought the following would work, but somehow it keeps telling me my array is either null, or not an object:

    Code:
    var field = new Array();
    
    function numberCells() {
    
    var theTable=document.all.oTable;
    
    for(var  i = 0; i < theTable.rows.length;i++){
        field[i]=theTable.rows(i).length;
    for(var j=0;j< theTable.rows(i).cells.length; j ++) {
        field[i][j]+=theTable.rows(i).cells(j).length;
            }
        }
    alert(field[i][j])
    }
    When i run the code, it says basically that the line in blue is null.
    LovesWar

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var field = new Array ();
    
    function numberCells ()
    {
      var theTable = document.all.oTable;
    
      for (var i = 0; i < theTable.rows.length; i++)
      {
        field[i] = theTable.rows(i).length;
        for (var j = 0; j < theTable.rows(i).cells.length; j++)
        {
          if (j = 0) field[i] = new Array ();
          field[i][j] += theTable.rows(i).cells(j).length;
        }
      }
      alert (field[i][j]);
    }
    You have to tell the fields[i] to be an array.

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I will be damn. I sort of knew, but wasnt totally certain. Makes sense though, since the first array only creates one row, as it were, but for each row, you must specify depth, or columns, and this requires that you create an array for the first index. A bit abstract though.

    Thankz a bill Aaron.
    LovesWar

  • #4
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Out of curiousity, why the testing of the j variable in your code?

    Code:
    var field = new Array ();
    
    function numberCells ()
    {
      var theTable = document.all.oTable;
    
      for (var i = 0; i < theTable.rows.length; i++)
      {
        field[i] = theTable.rows(i).length;
        for (var j = 0; j < theTable.rows(i).cells.length; j++)
        {
          if (j = 0) field[i] = new Array ();
          field[i][j] += theTable.rows(i).cells(j).length;
        }
      }
      alert (field[i][j]);
    }
    Couldnt have i just as easily done something like, "if (theTable) field[i] = new Array ();"?
    LovesWar

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    LOL...I just ran the code, and no green lights. LOL. I dont get it. The same message is popping up. field[..] is null, or not an object.
    LovesWar

  • #6
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure about your errors, will check that in a second, but the testing on j is so that you tell field[i] to be an array only once. To clean the code up, I've moved it into the for () loop's first argument:

    Code:
    var field = new Array ();
    
    function numberCells ()
    {
      var theTable = document.all.oTable;
    
      for (var i = 0; i < theTable.rows.length;i++)
      {
        field[i] = theTable.rows(i).length;
        for (var j = 0, field[i] = new Array (); j < theTable.rows(i).cells.length; j++)
          field[i][j] += theTable.rows(i).cells(j).length;
      }
      alert (field[i][j]);
    }

  • #7
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, I'm going to just take a guess: "field[i][j] +="

    You're trying to increment a variable that has no value as of yet. Try making it either just "field[i][j] =", or setting a default value for it in the for loop's first argument.

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var field =null;
    function numberCells ()
    {
      var theTable = document.all.oTable;//this as far as I know only works in IE
      field=new Array ();
      for (var i = 0; i < theTable.rows.length; i++)
      {
        field[i]=new Array ();
        for (var j = 0; j < theTable.rows(i).cells.length; j++)
        {
          field[i][j]="";//need to fill in some data here which presumably ain't the length.
        }
      }
      alert (field[i][j]);//j should be undefined here so that alert should fail
    }

  • #9
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To Garadon:

    I wish i could tell you that your code works, but unfortunately, it does not. It continues to tell me thaf field[] is null, or undefined. It simply will not understand anything.
    LovesWar

  • #10
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    IN
    Mozila it fails in the line I said was IE only so thats logical.

    IE it fails in the alert line which I also satd it would. in theory i and j should
    have vanished and be undefined in that line cause they should only exist within
    the for loops and the alert is after those. though alerting j or i at that lvl in IE
    gives the values of the arrays dimensions in there respective place plus 1.

  • #11
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, i wish i knew what to make of what yu said, cause according to one Perl script, i have mozilla, so i really dont know what to think.
    LovesWar


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •