Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2012, 11:13 PM   PM User | #1
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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!
triko is offline   Reply With Quote
Old 12-04-2012, 11:29 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-04-2012, 11:31 PM   PM User | #3
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
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..
Lerura is offline   Reply With Quote
Old 12-05-2012, 12:20 PM   PM User | #4
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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!
triko is offline   Reply With Quote
Old 12-05-2012, 12:27 PM   PM User | #5
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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);
devnull69 is offline   Reply With Quote
Old 12-05-2012, 08:34 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-05-2012, 08:37 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-05-2012, 11:12 PM   PM User | #8
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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!!!
triko is offline   Reply With Quote
Old 12-06-2012, 12:13 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-06-2012, 01:57 PM   PM User | #10
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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!!
triko is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:54 PM.


Advertisement
Log in to turn off these ads.