Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an array through a switch() statement in Javascript

I'm trying to develop a simplified poker game through Javascript. I've listed all possible card combinations a given player might have in its hand ordered by its value, like this:

switch(sortedHand)
{           
 //Pair
     case [1,1,4,3,2]: sortedHand.push(1,"Pair"); break;
     case [1,1,5,3,2]: sortedHand.push(2,"Pair"); break; 
     case [1,1,5,4,2]: sortedHand.push(3,"Pair"); break;
     case [1,1,5,4,3]: sortedHand.push(4,"Pair"); break;
     case [1,1,6,3,2]: sortedHand.push(5,"Pair"); break;
     case [1,1,6,4,2]: sortedHand.push(6,"Pair"); break;
     case [1,1,6,4,3]: sortedHand.push(7,"Pair"); break;
     case [1,1,6,5,2]: sortedHand.push(8,"Pair"); break;
     case [1,1,6,5,3]: sortedHand.push(9,"Pair"); break;
     case [1,1,6,5,4]: sortedHand.push(10,"Pair"); break;

Even though the "sortedHand" array stores values succesfully (as I've seen through console.log), the switch() statement always returns the default case, and everyone gets an straight flush. I fear this is a matter of the literal approach I've used to declare possible array values to be compared with the whole of "sortedHand", but I don't know any better. Is it even possible to use switch() in such a manner?

like image 501
Guilherme de Abreu Avatar asked Jul 23 '13 18:07

Guilherme de Abreu


People also ask

Can you use switch on an array in JavaScript?

you can use only one value at a time in switch statement because switch statement is meant for comparing one value with multiple option that's why we use it. if you want to use switch statement with all of values that are in array then you need to loop an array .

Can you use an array in a switch statement?

Yes, you can pass an array to a switch.

What is switch statement in JavaScript with example?

A switch statement compares the value of an expression to multiple cases. switch statements will check for strict equality. In this example, since "2"!== 2 , the default clause will execute.


2 Answers

You can try switching on a textual representation of the array.

switch(sortedHand.join(' '))
{           
    //Pair
    case '1 1 4 3 2': sortedHand.push(1,"Pair"); break;
    case '1 1 5 3 2': sortedHand.push(2,"Pair"); break; 
    case '1 1 5 4 2': sortedHand.push(3,"Pair"); break;
    case '1 1 5 4 3': sortedHand.push(4,"Pair"); break;
    // etc.
}

As an alternative to specifying every case directly, perhaps build a function dispatch table using an object and get rid of the switch entirely.

var dispatch = {};

// Build the table however you'd like, for your application
for (var i = 0; i < 10; i++) {
    (function(i) {
        var hand = ...; // Add your hand logic here
        dispatch[hand] = function() { sortedHand.push(i, "Pair"); };
    })(i);
}

// Execute your routine
dispatch[sortedHand.join(' ')]();
like image 53
voithos Avatar answered Oct 14 '22 20:10

voithos


the switch() statement always returns the default case

That's because the comparison doesn't check the array contents, but the array object itself. Objects are considered equal by their identity, so nothing will be equal to an object instantiated by a literal.

Is it even possible to use switch() in such a manner?

Yes, one can use objects in switch statements, but you would have to use references in the cases. Not applicable to your problem.

In your case, I'd suggest a stringification:

switch(sortedHand.join())
{           
 //Pair
     case "1,1,4,3,2": sortedHand.push(1,"Pair"); break;
     case "1,1,5,3,2": sortedHand.push(2,"Pair"); break; 
     case "1,1,5,4,2": sortedHand.push(3,"Pair"); break;
     case "1,1,5,4,3": sortedHand.push(4,"Pair"); break;
     case "1,1,6,3,2": sortedHand.push(5,"Pair"); break;
     case "1,1,6,4,2": sortedHand.push(6,"Pair"); break;
     case "1,1,6,4,3": sortedHand.push(7,"Pair"); break;
     case "1,1,6,5,2": sortedHand.push(8,"Pair"); break;
     case "1,1,6,5,3": sortedHand.push(9,"Pair"); break;
     case "1,1,6,5,4": sortedHand.push(10,"Pair"); break;

but I guess there's an even better, arithmetic solution to detect the patterns you're after. That would be shorter and faster, but I'm not sure what exactly this snippet is supposed to do.

like image 27
Bergi Avatar answered Oct 14 '22 20:10

Bergi