...

View Full Version : Problem array, if!



triko
12-04-2012, 11:13 PM
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


<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! :D

Old Pedant
12-04-2012, 11:29 PM
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]
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:


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.

Lerura
12-04-2012, 11:31 PM
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

triko
12-05-2012, 12:20 PM
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


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:


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:


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!
:D

devnull69
12-05-2012, 12:27 PM
Be careful


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


Math.floor(Math.random()*37);

Old Pedant
12-05-2012, 08:34 PM
Be careful


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


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".



var ball = Math.floor( Math.random() * 38 ) - 1;

Old Pedant
12-05-2012, 08:37 PM
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


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:


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!!!



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 ...

triko
12-05-2012, 11:12 PM
And we *GAVE* you the RIGHT ANSWER!!!



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!!! :D
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!!! :D

Old Pedant
12-06-2012, 12:13 AM
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.

triko
12-06-2012, 01:57 PM
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:


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!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum