Passing arrays to a function

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

    #1

    Passing arrays to a function

    I am trying to pass arrays of various lengths to a function.
    However the values are read as a string.
    What is the easiest way to pass the values?

    <script type="text/javascript">
    <!--
    function foo() {
    var args = "start ";
    for( var i=0 ; i < arguments.lengt h; i++ ) {
    if(i==0)
    args += arguments[i];
    else if(i%2) // odd
    args += ' (' + arguments[i];
    else
    args += ', ' + arguments[i] + ')';
    }args += ' end';
    return args;
    }

    myArray1 = new Array('x', 2, 3)
    myArray2 = new Array('y', 5, 7, 11, 13)

    alert(foo(myArr ay1)); // start 1,2,3 end
    alert(foo(myArr ay2)); // start 2,3,5,12,13 end

    // alerts should be: start 1 (2, 3) end
    // start 2 (3, 5) (12, 13) end
    // -->
    </script>
  • Lasse Reichstein Nielsen

    #2
    Re: Passing arrays to a function

    aliensite@excit e.com (aliensite) writes:
    [color=blue]
    > I am trying to pass arrays of various lengths to a function.
    > However the values are read as a string.
    > What is the easiest way to pass the values?
    >
    > <script type="text/javascript">
    > <!--
    > function foo() {
    > var args = "start ";
    > for( var i=0 ; i < arguments.lengt h; i++ ) {[/color]

    You call foo with *one* argument. That argument is an array
    containing values, but it is only one array.
    [color=blue]
    > if(i==0)
    > args += arguments[i];[/color]

    Here you add the array to a string, so it is converted to a string.

    Solutions:
    function foo(arr) { // and change "arguments" to "arr"
    or
    alert(foo.apply (this,myArray1) );

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Douglas Crockford

      #3
      Re: Passing arrays to a function

      > I am trying to pass arrays of various lengths to a function.[color=blue]
      > However the values are read as a string.
      > What is the easiest way to pass the values?
      >
      > <script>
      > function foo() {
      > var args = "start ";
      > for( var i=0 ; i < arguments.lengt h; i++ ) {
      > if(i==0)
      > args += arguments[i];
      > else if(i%2) // odd
      > args += ' (' + arguments[i];
      > else
      > args += ', ' + arguments[i] + ')';
      > }args += ' end';
      > return args;
      > }
      >
      > myArray1 = new Array('x', 2, 3)
      > myArray2 = new Array('y', 5, 7, 11, 13)
      >
      > alert(foo(myArr ay1)); // start 1,2,3 end
      > alert(foo(myArr ay2)); // start 2,3,5,12,13 end
      >
      > // alerts should be: start 1 (2, 3) end
      > // start 2 (3, 5) (12, 13) end
      > // -->
      > </script>[/color]

      You only use arguments when you have a variable number of arguments. In this
      case, you have a single argument, which will be an array.

      So replace the first line with

      function foo(a) {

      and replace all occurrences of 'arguments' with 'a'.



      Comment

      Working...