0
\$\begingroup\$

I am making a poker game and poker-evaluator that I am using to tell me only the outcome from the cards

'invalid hand',
    'high card',
    'one pair',
    'two pairs',
    'three of a kind',
    'straight',
    'flush',
    'full house',
    'four of a kind',
    'straight flush'

I am trying to know which cards in the sets are responsible for the "win".

I made a filter for duplicates and full house (i.e. three duplicates of same number and 2 duplicates of other number).

How can I improve the quality of the code below?

     interface CardDuplicate{
       Id:Array<number>
      } 
      const getDuplicates =(Firsts:Array<string>,FullArray:Array<string>)=> {
          let duplicates ={} as CardDuplicate;
          for (let i = 0; i < Firsts.length; i++) {
       
        const key = Firsts[i] as keyof  CardDuplicate
          if(key in duplicates) {
         
              duplicates[key].push(i);
          }else if (Firsts.lastIndexOf(key) !== i) {
              duplicates[key] = [i];
          }
        }
          
         let Type = "none";
         if(duplicates === undefined){
    
          return {Type:Type,Cards:[]};
         }
           const Obj = Object.keys(duplicates).map((key)=> {
        key = key as keyof CardDuplicate
        if(duplicates !== undefined && duplicates[key] !== undefined){
  if(duplicates[key ].length > 2 && Object.keys(duplicates).length > 1){
    Type= "Full house";
  }else if( Object.keys(duplicates!).length > 2){
    Type= "three pairs";
  }else if(duplicates[key].length == 2 && Object.keys(duplicates).length > 1){
    Type= "two pairs";
  }else if(duplicates[key].length == 2 && Object.keys(duplicates).length == 1){
    Type= "one pairs";
  }
   return duplicates[key ].map((data:string)=>{return FullArray[data]});
}else{
  return []
}
          });
        
          return {Type:Type,Cards:Obj};
        
      };
    function CheckIfPairs(Item:Array<string>){
    
      const Firsts:Array<string>=[];
      const Letters:Array<string>=[];
     
     Item.map((data:string)=>{
      const Number:string = data.split('')[0]
       Firsts.push(Number);
       Letters.push(data.split('')[1])
     })
    return getDuplicates(Firsts,Item)
    }
    
  

    //s = spades h = hearts c = clubs (♣), d= diamonds
         console.log(CheckIfPairs([
              "2s",
              "2d",
              "6c",
              "3s",
              "8d",
              "4h", 
              "9c",
            ]))
\$\endgroup\$
1
  • \$\begingroup\$ Please start by autoformatting your code (but only before someone writes an answer) (hint: select the --parser typescript option). \$\endgroup\$
    – ggorlen
    Oct 25, 2022 at 15:49

1 Answer 1

1
\$\begingroup\$

Yes, please format the code, so it's easier to read.

Look at your duplicates === undefined. These seem overly-cautious checks. How can these occur?

It also seems like Object.keys(duplicates).length is assumed to be the number of duplicates. But it seems like it is a reverse index of cards, and there may just be a single card in the entries, so some of the logic may be off.

Hope this helps.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.