Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Problem array, if!

    HI all! I have the next problem!
    I don't know how to translate my thought in code javascript!
    My thought is: I would use an array into an if in this mode
    Code:
    <html>
    <head>
    </head>
    <script type = "text/javascript">
    function number (foo)
    {
        var back = false;
        var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        if (foo == myArray [3])
        {
            back = true;
        }
        return back;
    }
    function start ()
    {
        var foo = 3; //random number
        var hello = number (foo);
    }
    </script>
    <body>
    <button id = "ciao" onclick = "start()"> TRY ME </button>
    </body>
    </html>
    So, I want to say, verify that my variable that is into my array!!! Is it possible???? And before my var false, change in true!!! But i want to verify that my var foo = 3 is it into my Array!!!
    I hope thats clear!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,509
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    You mean *ANYWHERE* in the array???

    In other words, if you search for the value 3 and it is in *ANY* element of the array then you want to return true? else false?

    You don't even need a function: [code]
    Code:
    var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    var findit = ( myArray.indexOf(3) >= 0 );  //  will put true into findit
    or
    var findit = ( myArray.indexOf(731) >= 0 ); // will put false into findit
    *EXCEPT*

    Except older MSIE browsers don't have the indexOf method on arrays.

    But that's easy to fix:
    Code:
    if ( Array.prototype.indexOf == null )
    {
        Array.prototype.indexOf = 
            function( lookFor )
            {
                for ( var i = 0; i < this.length; ++i ) 
                {
                    if ( this[i] == lookFor ) return i;
                }
                return -1;
          }
    }
    Now Array.indexOf( ) will be there whatever browser you use.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    945
    Thanks
    0
    Thanked 129 Times in 128 Posts
    You can test if the value of foo is equal to one of the values in you array by:

    var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    function number(foo){
    return (myArray.indexOf(foo!=-1);
    }

    Old P. was a little faster than me
    Last edited by Lerura; 12-04-2012 at 11:34 PM.

  • #4
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OK! as usual I haven't explained...
    I have the problem of the game roulette...
    When click start, put out one number random from 0 to 36
    Code:
    var ball = Math.floor(Math.random()*36);
    before I have all my bets, example corners, split, red or black ecc... My problem born for corner, split, column, dozen ecc... Because I have all array of the possible combination, example columns:
    Code:
           var col1 = [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34];
           var col2 = [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35];
           var col3 = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36];
    Ok now in my css part I selected col1 and start the game... ball = 16;
    Now I don't know how tell that the number 16 is into col1!
    Other example is for split, there are so many, from split1 to split60:
    Code:
           var split1 = [0, 1];
           var split2 = [0, 2];
           var split3 = [0, 3];
           var split4 = [1, 2];
           var split5 = [2, 3];
           var split6 = [1, 4];
           var split7 = [2, 5];
           var split8 ........... ecc
    Here have more combination possible and I don't know how to say if the ball = 0, if split 1 or 2 or 3!

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Be careful
    Code:
    Math.floor(Math.random()*36);
    will never have the value "36". Math.random() will result in values between 0 and 1, including 0 but excluding(!) 1. So you will have to use
    Code:
    Math.floor(Math.random()*37);

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,509
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    Quote Originally Posted by devnull69 View Post
    Be careful
    Code:
    Math.floor(Math.random()*36);
    will never have the value "36". Math.random() will result in values between 0 and 1, including 0 but excluding(!) 1. So you will have to use
    Code:
    Math.floor(Math.random()*37);
    Ummm...but roulette *ALSO* has a "00" spot (at least on American roulette wheels). So thare are actually 38 spots.

    But of course "00" and "0" are the same number, when viewed as a number. So I'd simply make the random number be -1 to 36. And then -1 means "00".

    Code:
    var ball = Math.floor( Math.random() * 38 ) - 1;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,509
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    Quote Originally Posted by triko View Post
    OK! as usual I haven't explained...
    I have the problem of the game roulette...
    When click start, put out one number random from 0 to 36
    Code:
    THIS CODE IS WRONG AS NOTED BY WOLFSHADE 
    var ball = Math.floor(Math.random()*36);
    before I have all my bets, example corners, split, red or black ecc... My problem born for corner, split, column, dozen ecc... Because I have all array of the possible combination, example columns:
    Code:
           var col1 = [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34];
           var col2 = [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35];
           var col3 = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36];
    ...
    And we *GAVE* you the RIGHT ANSWER!!!

    Code:
    if ( col1.indexOf( ball ) >= 0 ) 
    {
        // ball is in col1
    } else if ( col2.indexOf( ball ) >= 0 ) {
        // ball is in col2
    } else if ( col3.indexOf( ball ) >= 0 ) {
        // ball is in col3
    } else ... other tests ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    And we *GAVE* you the RIGHT ANSWER!!!

    Code:
    if ( col1.indexOf( ball ) >= 0 ) 
    {
        // ball is in col1
    } else if ( col2.indexOf( ball ) >= 0 ) {
        // ball is in col2
    } else if ( col3.indexOf( ball ) >= 0 ) {
        // ball is in col3
    } else ... other tests ...
    The zero now, doesn't more important! ( So thanks for advice)
    ok, but I have 60 splits xD, my teacher tell me, if the code have more and more string of the same code, you have a best solution!!!
    Because write 60 split, it's hard!!!
    isn't possible use one array that content all 60 split, and before start an FOR CYCLE for verify that into my array have the number???
    I think every night about how to do!!!

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,509
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    But if this truly is a roulette simulation, then you need to *ALSO* determine what the PAYOFF is for win. That is, if the ball really is found in one of your arrays, what does the player get paid?

    So I think you need to account for *ALL* of that.

    I think (not sure) that this is pretty easy: I think that in roulette the payoff is always based on the number of choices available on the "bet" the user made.

    That is, the payoff for the ball being in [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34] is 3 for 1 because there are 12 elements to that array and 36/12 is 3.

    Similarly, for a match on [0, 1] the payoff is 18 for 1, because there are 2 elements in that array and 36/2 is 18.

    No?

    As for generating ALL the possible splits: First of all, you only need the splits that one or more players have actually bet on. So I'd be tempted to use JS code to *generate* only those bet-on splits. For other bets (red, black, column, 6-group, etc.) I'd probably create the groups ahead of time.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post

    That is, the payoff for the ball being in [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34] is 3 for 1 because there are 12 elements to that array and 36/12 is 3.

    Similarly, for a match on [0, 1] the payoff is 18 for 1, because there are 2 elements in that array and 36/2 is 18.

    No?
    time.
    Don't understand this part, but I know the value of various bet on, Example if number of ball is your bet on, the money that you put is multiplied for 35!
    If you bet on red or black is multiplied for 2, if bet on columns is for 5!!!
    I have structured my problem do it:
    Code:
        function start ()
        {
           var ball = Math.floor (Math.random ()*36);
           // Write all var for Numbers
           var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36];
           // Write all var for Even bets
           var red = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 19, 21, 23, 25, 27, 30, 32, 34, 36];
           var black = [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35];
           var odd = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35];
           var even [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36];
           var manque = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
           var passe = [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35];
           // Write all var for Dozens
           var ereFirts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
           var ereSecond = [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24];
           var ereThird = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35];
           // Write all var for Columns
           var col1 = [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34];
           var col2 = [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35];
           var col3 = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36];
           // Write all var for Lines or Double Streets
           var lin1 = [1, 2, 3, 4, 5, 6];
           var lin2 = [4, 5, 6, 7, 8, 9];
           var lin3 = [7, 8, 9, 10, 11, 12];
           .........................
           // Write all var for Corners
           var cor1 = [0, 1, 2, 3];
           var cor2 = [1, 2, 4, 5];
           var cor3 = [2, 3, 5, 6];
           var cor4 = [4, 5, 7, 8];
           var cor5 = [5, 6, 8, 9];
           .......................
    
           // Write all var for Streets
           var strt1 = [0, 1, 2];
           var strt2 = [0, 2, 3];
           var strt3 = [1, 2, 3];
           var strt4 = [4, 5, 6];
           var strt5 = [7, 8, 9];
           var strt6 = [10, 11, 12];
           ......................
           // Write all var for Split
           var split1 = [0, 1];
           var split2 = [0, 2];
           var split3 = [0, 3];
           var split4 = [1, 2];
           var split5 = [2, 3];
           var split6 = [1, 4];
           var split7 = [2, 5];
           var split8 = [3, 6];
           var split9 = [4, 5];
           var split10 = [5, 6];
           //Now i don't know how to do for verify if the number of var ball is into that one of this ARRAY!
    I use simple css with checkbox for rappresent all possibility, so You choose How much put into check and before check what do you want: columns, number single, spli, red, ecc.....!!!
      }
    It's complicate!! sad problem!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •