Arrays and Functions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Arrays and Functions

  1. #1
    Matt Guest

    Arrays and Functions


    I have an array which contains the names of functions (a1() and a2()) and
    a function is randomly picked. My problem is that the function name is stored
    in a variable (choosescript) and I cannot work out how to run the function.
    Here is the script:

    function mine() {
    var scriptlist = new Array;
    var choosescript;
    scriptlist[0] = "a1"
    scriptlist[1] = "a2"
    choosescript = scriptlist[Math.floor(Math.random() * scriptlist.length)];

    Any ideas please?

    Matt (Matt0220@AOL.com)

  2. #2
    Paul Clapham Guest

    Re: Arrays and Functions

    if (choosescript.equals("a1")) a1();
    if (choosescript.equals("a2")) a2();

    There are other ways to do this, but they would require more changes.

    PC2

    Matt <matt0220@aol.com> wrote in message news:39ccdeac$1@news.devx.com...
    >
    > I have an array which contains the names of functions (a1() and a2()) and
    > a function is randomly picked. My problem is that the function name is

    stored
    > in a variable (choosescript) and I cannot work out how to run the

    function.
    > Here is the script:
    >
    > function mine() {
    > var scriptlist = new Array;
    > var choosescript;
    > scriptlist[0] = "a1"
    > scriptlist[1] = "a2"
    > choosescript = scriptlist[Math.floor(Math.random() * scriptlist.length)];
    >
    > Any ideas please?
    >
    > Matt (Matt0220@AOL.com)




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center