passing parameter of a function to array

Discussion in 'Javascript' started by cuco, Feb 22, 2011.

  1. cuco

    cuco Guest

    I want to use the parameter of a function as a variable for an array.

    Example:
    <html>
    <head>
    my function (parameter){
    x=new Array(parameter);
    // rest of the code using x
    }
    </head>
    <body>
    <script language="javascript">my function(" ' item1' , ' item2' , '
    item3' ")</script>
    </body>
    </html>

    It's not working.
     
    cuco, Feb 22, 2011
    #1
    1. Advertising

  2. cuco wrote:

    > I want to use the parameter of a function as a variable for an array.
    >
    > Example:
    > <html>
    > <head>
    > my function (parameter){
    > x=new Array(parameter);
    > // rest of the code using x
    > }
    > </head>
    > <body>
    > <script language="javascript">my function(" ' item1' , ' item2' , '
    > item3' ")</script>
    > </body>
    > </html>


    Your code is strewn with syntax errors.

    > It's not working.


    <http://jibbering.com/faq/notes/posting/#ps1DontWork>
    <http://validator.w3.org/>
    <http://getfirebug.com/>


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
     
    Thomas 'PointedEars' Lahn, Feb 22, 2011
    #2
    1. Advertising

  3. cuco

    Erwin Moller Guest

    On 2/22/2011 4:28 PM, cuco wrote:
    > I want to use the parameter of a function as a variable for an array.
    >
    > Example:
    > <html>
    > <head>
    > my function (parameter){
    > x=new Array(parameter);
    > // rest of the code using x
    > }
    > </head>
    > <body>
    > <script language="javascript">my function(" ' item1' , ' item2' , '
    > item3' ")</script>
    > </body>
    > </html>
    >
    > It's not working.


    Oh my, you make a load of mistakes.

    1) "my function" is not a legal function name.
    2) You forgot to place javascript tags around the part in the header.
    3) Stop using language="javascript". Use: type="text/javascript" instead.
    4) Use a ; to end a statement.
    5) Do not try to pass around the array definition. Pass around a
    reference to the array.
    6) Get in the habbit of using var. You are poluting the global namespace
    without it.

    A lot more can be said, but I stop here.


    So, for starters, try this:

    <html>
    <head>
    <script type="text/javascript">
    myFunction (anArray){
    var x=anArray;
    // The above is probably nonsense because you could use
    // anArray too, instead of x. :)
    // rest of the code using x
    }
    </script>

    </head>
    <body>

    <script type="text/javascript">
    var myArray = ['item1' , 'item2' , 'item3'];
    myFunction(myArray);
    </script>
    </body>
    </html>


    Not tested.
    Erwin Moller


    --
    "That which can be asserted without evidence, can be dismissed without
    evidence."
    -- Christopher Hitchens
     
    Erwin Moller, Feb 22, 2011
    #3
  4. cuco

    VK Guest

    On Feb 22, 6:28 pm, cuco <> wrote:
    > I want to use the parameter of a function as a variable for an array.


    As Pointed out, my function() {... is either an error or a typo for
    JavaScript

    For the rest your actual aim is not clear. If you need a function that
    takes any amount of arguments and the exact amount may change at
    runtime then use automatically created for you "arguments" list:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Test</title>
    <meta http-equiv="Content-Type"
    content="text/html; charset=utf-8">
    <script>
    function myFunction() {
    for (var i=0; i<arguments.length; i++) {
    window.alert(arguments);
    }
    }
    </script>
    </head>
    <body>
    <p>
    <button type="button" onclick="
    myFunction('a', 'b');
    ">Call with 2 args</button>
    </p>
    <p>
    <button type="button" onclick="
    myFunction('a', 'b', 'c');
    ">Call with 3 args</button>
    </p>
    </body>
    </html>

    If you need a function that - as in your example - gets a single
    string containing comma-delimited values, parses that string and
    stores each found value in an array then it is a whole different
    question.
     
    VK, Feb 22, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nitinm
    Replies:
    2
    Views:
    340
    Barry Schwarz
    Sep 14, 2007
  2. Mister B
    Replies:
    8
    Views:
    558
    Nick Keighley
    Aug 26, 2010
  3. Kai-Uwe Bux
    Replies:
    2
    Views:
    328
    Luc Danton
    Oct 3, 2010
  4. Luc Danton
    Replies:
    2
    Views:
    730
    Luc Danton
    Oct 4, 2010
  5. AzamSharp
    Replies:
    2
    Views:
    181
Loading...

Share This Page