passing array to new fuction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • windandwaves

    #1

    passing array to new fuction

    Hi Folk

    I have the following function
    var a = new Array(4);
    a[0] = new Array(45); //status (on or off)
    a[1] = new Array(45); //name of the region
    a[2] = new Array(45); //on mouse over
    a[3] = new Array(45); //on mouse out

    a[0][0] = false;
    .....snip
    a[1][0] = "region name";
    ......snip
    a[2][1] = new Array('x', 'b','c','d',); // outer islands
    a[2][2] = new Array('h', 'x', 'x', 'n');
    ....snip
    a[3][1] = new Array('a', 'b','c','d',); // outer islands
    a[3][2] = new Array('h', 'h', 'n', 'n');
    ....snip

    function Rx(i) {
    var j;
    var dd = new Array();
    document.getEle mentById('r'+i) .checked ? j = 3 : j = 2;
    dd = a[j][i];
    si(dd);
    }

    function si() {
    if (document.image s && (preloadFlag == true)) {
    var img;
    for (var i=0; i<si.arguments. length; i+=2) {
    img = null;
    if (document.layer s) {
    img = findElement(si. arguments[i],0);
    }
    else {
    img = document.images[si.arguments[i]];
    }
    if (img) {
    img.src = si.arguments[i+1]+'.gif';
    }
    }
    }
    }

    My problem is when I call the Rx(i) function, it passes the whole array (e.g. a[2][3]) as one variable to si and not an array,
    therefore, si does not work. What am I doing wrong?

    There are about 200 a[i][j] variables, some of which are arrays in themselves.

    TIA

    - Nicolaas



  • RobG

    #2
    Re: passing array to new fuction

    windandwaves wrote:[color=blue]
    > Hi Folk[/color]
    [...][color=blue]
    > ...snip
    > a[3][1] = new Array('a', 'b','c','d',); // outer islands[/color]

    You have a syntax error here -------^
    [color=blue]
    > a[3][2] = new Array('h', 'h', 'n', 'n');
    > ...snip
    >
    > function Rx(i) {
    > var j;
    > var dd = new Array();
    > document.getEle mentById('r'+i) .checked ? j = 3 : j = 2;
    > dd = a[j][i];
    > si(dd);[/color]

    Insert:

    alert(dd);

    to see that dd is an array.
    [color=blue]
    > }
    >
    > function si() {
    > if (document.image s && (preloadFlag == true)) {
    > var img;
    > for (var i=0; i<si.arguments. length; i+=2) {
    > img = null;
    > if (document.layer s) {
    > img = findElement(si. arguments[i],0);
    > }
    > else {
    > img = document.images[si.arguments[i]];[/color]

    Here you are grabbing the arguments, but there is only one - the
    entire array of what was formerly dd. Try:

    ...si.arguments[0][i]];

    [...]

    No warranty or guarantees provided.

    --
    Rob

    Comment

    • RobB

      #3
      Re: passing array to new fuction

      RobG wrote:[color=blue]
      > windandwaves wrote:[color=green]
      > > Hi Folk[/color]
      > [...][color=green]
      > > ...snip
      > > a[3][1] = new Array('a', 'b','c','d',); // outer islands[/color]
      >
      > You have a syntax error here -------^
      >[color=green]
      > > a[3][2] = new Array('h', 'h', 'n', 'n');
      > > ...snip
      > >
      > > function Rx(i) {
      > > var j;
      > > var dd = new Array();
      > > document.getEle mentById('r'+i) .checked ? j = 3 : j = 2;
      > > dd = a[j][i];
      > > si(dd);[/color]
      >
      > Insert:
      >
      > alert(dd);
      >
      > to see that dd is an array.
      >[color=green]
      > > }
      > >
      > > function si() {
      > > if (document.image s && (preloadFlag == true)) {
      > > var img;
      > > for (var i=0; i<si.arguments. length; i+=2) {
      > > img = null;
      > > if (document.layer s) {
      > > img = findElement(si. arguments[i],0);
      > > }
      > > else {
      > > img = document.images[si.arguments[i]];[/color]
      >
      > Here you are grabbing the arguments, but there is only one - the
      > entire array of what was formerly dd. Try:
      >
      > ...si.arguments[0][i]];
      >
      > [...]
      >
      > No warranty or guarantees provided.
      >
      > --
      > Rob[/color]

      Two syntax errors.
      Not much point to this:

      function Rx(i) {
      var j;
      var dd = new Array();
      document.getEle mentById('r'+i) .checked ? j = 3 : j = 2;
      dd = a[j][i];
      si(dd);
      }

      You've created a new array, assigned it to dd, and then overwritten
      that with whatever was in the element a[j][i]. Why not just pass the
      original reference?

      si(a[j][i]);

      As RobG hinted, you can't replace the arguments array (btw
      'Function.argum ents' was deprecated in favor of simply 'arguments'
      around 1998) with another array passed in there; it'll be treated as
      one argument.

      function Rx(i) {
      var el;
      if (el = document.getEle mentById('r' + i))
      si(a[el.checked ? 3 : 2][i]);
      }

      function si(arr) {
      if (document.image s
      && preloadFlag
      && typeof arr[0] != 'undefined') {
      var img;
      for (var i=0; i<arr.length; i+=2) {
      img = null;
      if (document.layer s) {
      img = findElement(arr[i],0);
      }
      else {
      img = document.images[arr[i]];
      }
      if (img) {
      img.src = arr[i+1]+'.gif';
      }
      }
      }
      }

      This sort of script is often made dramatically clearer (and more
      efficient) by replacing the nested arrays with some sort of generic
      object structure using named properties.

      Comment

      Working...