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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to return 2 dimensional array

    hello,

    I'm rather new in JS. Can somebody help me?.
    I try to return 2 demensional array from the function like this:
    <SCRIPT LANGUAGE=Javascript 1.2>
    function 1
    {
    elements=new Array();
    for (i=0;i<7;i++)
    { function2(elements[i],i)
    }
    return (elements);
    }

    function 2(str,num)
    { elements[num]=new Array();
    for (i=0;i<7;i++)
    { elements[elements[num][i]=i;
    }
    }

    var na=new Array(new Array());
    na=function1;
    for (i=0;i<7;i++)
    { for (j=0;i<7;i++)
    { alert(na[i][j]);}
    }
    //As I understand, elements is global array...
    And it gives me an error (elements, na are empty or are not an objects). Could you tell me where my problem is?

  • #2
    Regular Coder
    Join Date
    Dec 2002
    Location
    Minneapolis, MN
    Posts
    208
    Thanks
    0
    Thanked 1 Time in 1 Post
    this may not solve your problem but I always though a 2-d array was such.

    Code:
      myArray = new Array(
        new Array("Item 1a", "Item 1b"),
        new Array("Item 2a", "Item 2b")
      );
    
    //the you reference one item like this
    
    sItem = myArray[1][1];
    
    //now sItem should be "Item 2b"
    I'm up for learning something new, so go ahead and tell me im wrong if im wrong.
    anthony

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    From what I can tell, elements is actually declared in function1() (rather function 1... why do you have spaces in your function names? That's "illegal").

    That's what I see as being wrong anyway..
    Code:
     var elements=new Array();
    
     function function1()  {
      for (var i=0;i<7;i++) { 
       function2(elements[i],i);
      }
    
      return (elements);
     }
    
     function function2(str,num) {
      elements[num]=new Array();
      
      for (var i=0;i<7;i++) { 
       elements[num][i]=i;
      }
     }
    
     var na=new Array(new Array());
     na=function1();
     for (var i=0;i<7;i++) { 
      for (var j=0;j<7;j++) { 
       alert(na[i][j]);
      }
     }
    I've highlight your errors that I saw in red. Hopefully that works for you.
    Sadiq.

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    Here is some problem with your script
    Code:
     1: function 1
     2: {
     3: elements=new Array();
     4: for (i=0;i<7;i++)
     5: { function2(elements[i],i)
     6: }
     7: return (elements);
     8: }
     9:
    10: function 2(str,num)
    11: { elements[num]=new Array();
    12: for (i=0;i<7;i++)
    13: { elements[elements[num][i]=i;
    14: }
    15: }
    16: 
    17: var na=new Array(new Array());
    18: na=function1;
    19: for (i=0;i<7;i++)
    20: { for (j=0;i<7;i++)
    21: { alert(na[i][j]);}
    22: }
    1: this is an invalid command. it appears as though you are trying to declare a function. use this: function function1()
    5: pass by reference, not by value. try: function2(elements,i);
    10: same deal. use: function function2()
    10: you appear to be passing an unused variable str.
    11: do you mean str[num] = new Array();
    13: you've got too many [ and not enough ]. (three opening square braces vs. two closing square braces.
    13: do you mean str[num][i] = i;
    15: you're not returning any value. the work you did in function2() will be lost.
    17: this is an invalid call to Array(). Array() expects an integer or a comma-separated list of values.
    18: you are replacing your newly declared array with a function. JavaScript will allow you to reuse the name function1, but your array is lost in the process.
    20: if you're trying to loop j, you need to have j in all three spots in the for structure: for(j=0;j<7;j++)
    21: sidestepping a potential headache. there are better ways to output than alert(). you stand to see 49 annoying alert() popups (along with the accompanying noise if your OS does that) if this actually works.

    Go over these notes. Think about the work you're trying to do. Let me know if you have further questions.

    Edit: darn it!! beaten again.

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kansel
    Edit: darn it!! beaten again.
    Both here and abroad.

    Well, I suppose you needed the practice.
    Nice summation tho....

    .....Willy

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ez4me2c3d
    this may not solve your problem but I always though a 2-d array was such.

    Code:
      myArray = new Array(
        new Array("Item 1a", "Item 1b"),
        new Array("Item 2a", "Item 2b")
      );
    
    //the you reference one item like this
    
    sItem = myArray[1][1];
    
    //now sItem should be "Item 2b"
    I'm up for learning something new, so go ahead and tell me im wrong if im wrong.
    Like everything else in computing, there's usually more than one way to accomplish something. n-dimensional arrays is one of them.

    ez4me2c3d, you've pointed out one way. Another way is like this:
    Code:
     var myArray = new Array();
    
     myArray [0] = new Array();
     myArray [0][0] = "Item 1a";
     myArray [0][1] = "Item 1b";
    
     myArray [1] = new Array();
     myArray [1][0] = "Item 2a";
     myArray [1][1] = "Item 2b";
    
     var sItem = myArray[1][1];

  • #7
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or using bracket notation:

    Code:
      var myArray = [["Item 1a" , "Item 1b"],
                     ["Item 2a" , "Item 2b"]]


  •  

    Posting Permissions

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