Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all possible combinations from two array

Imagine we have two arrays like below

let result = []
let array1 = ['2h', '3h', '4h', '5d', '8d']
let array2 = ['Kd', 'Ks']

I need to take each element from array2 and replace each element into array one by one and store in result

Result should be like this

result = ['Kd', '3h', '4h', '5d', '8d']
         ['2h', 'Kd', '4h', '5d', '8d']
         ['2h', '3h', 'Kd', '5d', '8d']
         .....
         ['Ks', '3h', '4h', '5d', '8d']
         ['2h', 'Ks', '4h', '5d', '8d']
         ['2h', '3h', 'Ks', '5d', '8d']
         .....

And also replace whole array2 in array1

result = ['Kd', 'Ks', '4h', '5d', '8d']
         ['2h', 'Kd', 'Ks', '5d', '8d']
         .....
         ['Kd', 'Ks', '4h', '5d', '8d']
         ['Kd', '3h', 'Ks', '5d', '8d']
         ['Kd', '3h', '4h', 'Ks', '8d']
         .....

This my current code, need to analyze poker hand for each player and then sort by strength

let ranks = ['2','3','4','5','6','7','8','9','T','J','Q','K','A']
let suits = ['h','d','c','s']
let gameTypes = ['texas-holdem','omaha-holdem','five-card-draw']
//Lets learn poker rules
// Card == table + player
let royalFlush
let straightFlush
let fourKind
let fullHouse
let royal
let straight
let threeKind
let twoPair
let twoKind
let highCard
var combos = []

function variants(table, player) {
    let result = []
    let tableArr = table.split(/(?=(?:..)*$)/)
    let playerArr = player.split(/(?=(?:..)*$)/)
    playerArr.forEach((item) => {
        for(let i = 0; i < tableArr.length; i++) {                                              
            const tempArray = [...tableArr];                                                           
            tempArray[i] = item;                                                                     
            result.push(tempArray);                                                                  
        }                                                                                          
    });                                                                                          
    return result 
}
function sortHand(input) {
    let handAnal = input.split(' ')
    handAnal.forEach((e, index) => {
        if(index == 0 || index == 1) {
            return
        } else {
            combos.push(variants(handAnal[1], handAnal[index]))
        }
    })
}

//console.log(sortHand('texas-holdem 4cKs4h8s7s Ad4s Ac4d As9s KhKd 5d6d'))
sortHand('texas-holdem 2h3h4h5d8d 9hJh')
console.log(combos);
//console.log(sortHand('omaha-holdem 3d3s4d6hJc Js2dKd8c KsAsTcTs Jh2h3c9c Qc8dAd6c 7dQsAc5d'))
//console.log(sortHand('five-card-draw 7h4s4h8c9h Tc5h6dAc5c Kd9sAs3cQs Ah9d6s2cKh 4c8h2h6c9c'))
like image 574
Levan Avatar asked Nov 23 '25 19:11

Levan


1 Answers

Here's my solution for it.

let result = [];                                                                             
let array1 = ["2h", "3h", "4h", "5d", "8d"];                                                 
let array2 = ["Kd", "Ks"];                                                                   
                                                                                               
array2.forEach((item) => {                                                                                for(let i = 0; i < array1.length; i++) {                                              
  const tempArray = [...array1];                                                           
  tempArray[i] = item;                                                                     
  result.push(tempArray);                                                                  
  }                                                                                          
});                                                                                          
                                                                                      
console.log(result); 

Here, we're iterating over the array2 and taking each item. And for every item of the array2 we're iterating over the first array and making a copy of it and storing it in the tempArray. After that we put the current item of the first array and put it the tempArray at the index i from the for loop.

like image 196
h-sifat Avatar answered Nov 25 '25 11:11

h-sifat