www.webdeveloper.com
Results 1 to 4 of 4

Thread: extracting item from an array

  1. #1
    Join Date
    Nov 2005
    Location
    Bundanoon: first village to ban bottled water
    Posts
    214

    extracting item from an array

    This randomising function is like a coin toss, it gives a numerical result and displays it:

    function Ran() {
    var c = Math.random();
    return (c < 0.5) ? [0, show0()] : 1, show1()];
    }

    I want to extract the numerical outcome, 0 or 1, from this and from two other similar functions, sum them, and then run more functions based on the possible sums.

    What is the syntax for getting to the numbers? something like

    var n = Ran().[0] ?

  2. #2
    Join Date
    May 2011
    Posts
    194

    maybe this

    Hi,

    function Ran()
    {
    var arReturn=new Array
    (
    1,
    show1()
    );

    if(Math.random()< 0.5)
    {
    arReturn[0]--;
    arReturn[1]=show0();
    }

    return arReturn;
    }


    alert(Ran()[0]);

    ----------------------------

    var n = Ran().[0] --> Ran() must return a pointer of an object with property array
    but Ran() is a pointer to code (return pointer of array).

  3. #3
    Join Date
    Feb 2006
    Posts
    2,926
    You can simplify things by rounding the random number to return 0 or 1.

    Code:
    function flip() { 
             return Math.round(Math.random()) 
    }
    alert(flip())

  4. #4
    Join Date
    Nov 2005
    Location
    Bundanoon: first village to ban bottled water
    Posts
    214
    thanks to both

    getting the hang of it...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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



Recent Articles