Johann Behrens
Johann Behrens

Reputation: 124

Comparing the last value of two multidimensional arrays

I am working on a very simple form of a Roulette in JavaScript, object oriented. I set up a board of 8 (red 1, white 2, red 3, white 4 ... white 8) and I pick a random position where the ball would fall with Math.random.

this.randomPick = function() { 
      var randomPos = Math.floor(Math.random() * (8 - 1) + 1); 
      var selectedPos = this.board[randomPos]; 
      console.log(selectedPos); 
};

I also ask the user to fill in a colour and a number (red or white, 1 - 8).

But then I am stuck, how can I compare if the user's input is equal to the place where the ball fell?

this.board = [["rood", 1], ["wit", 2], ["rood", 3], ["wit", 4], ["rood", 5], ["wit", 6], ["rood", 7], ["wit", 8]];

this.userInput = function(){
    var inzet = E("inzet").value;
    var kleur = E("kleur").value;
    var nummer = E("nummer").value;

    this.userIn.push(kleur, nummer);
};

Upvotes: 0

Views: 75

Answers (2)

Sami
Sami

Reputation: 8419

Two step solution. 1.Push the the user input color and value into a new array ar. 2.compare that that array with all elements of your two dim array.

ar = new Array();
ar.push(user_chosen_color);
ar.push(user_chosen_value);

for(i=0;i<two_dim_array.length;i++)
{
    if(ar==two_dim_array[i])
    {
        // Do what you want to do
    }
}

Update

ar = new Array();
ar.push(user_chosen_color);
ar.push(user_chosen_value);


function caompare(ar , two_dim_array)
{        
    var res = "Nothing";
    for(i=0;i<two_dim_array.length;i++)
    {
         if(ar[0]==two_dim_array[i][0])
         {
             res = "Color";
             if(ar[1]==two_dim_array[i][1])                 
                 res = "Both";                 
         }
         else
         {
              if(ar[1]==two_dim_array[i][1])                 
                 res = "Value";
         }
    }
    return res;
}
var res = compare(ar, two_dim_array);
// Now you have result in res

Upvotes: 1

Ryan Sanders
Ryan Sanders

Reputation: 151

8 reds + 8 blacks = 16 total possibly choices. 

//red
array[1] = 1;
...
array[8] = 8;
//black
array[9] = 1;
array[10] = 2;
....
array[16] = 8;

var user_number = 1; //0 even 1 odd
var user_color = 0; //0 black 1 red
var tempa = 0;

for (var loopa = 1; loopa <= 8; loopa++){
  if( user_color == 0){ tempa = loopa + 8; } else { tempa = loopa;} //black or red
     if ( user_number == array[tempa]){ //checks number
     //do something
     }
  }
}

================ for each type of winning for rolute would be another for/if statments.

get "google chrome" browser load up your webpage... top right corner click on the 3 bars icon goto down to TOOLS then click JAVASCRIPT CONSLE

you can type in say... array and then click arrows to see listings of array variables or see info about say... user_number to see what it is currently holding.

might help visualize things

Upvotes: 0

Related Questions