Search code examples
javascriptfunctionparameterspoker

Ranking Poker Hand


I'm building a function PokerHand() which should take a 5 card hand as a string and score them according to Texas Holdem Rules. I've written the code so that it's first sorted according to rank. So that the hand const handOne = ('AC 4S 5S 8C AH') becomes let sortedHandOne = ["4S", "5S", "8C", "AC", "AH"] (which works), and then gets splits into an array of rank and corresponding suits (which isn't working). When I run the suitsAndRank function I am expecting rankArray = ["4", "5", "8", "A", "A"] and suitArray = ["C", "C", "H", "S", "S"] but I get empty arrays. I can't figure out why.

Here's the code:

function PokerHand() {
    //get ranks of hands 

    const handOne = ('AC 4S 5S 8C AH');
    //const handTwo = ('4S 5S 8C AS AD');

    let rankArray = [];
    let suitArray = [];

    // let rankArrayTwo = [];
    // let suitArrayTwo = [];

    const suits = ["C", "D", "H", "S"]
    const ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]

    let arrayHandOne = handOne.split(" ");
    //let arrayHandTwo = handTwo.split(" ");

    function sorted() {
    let sortedHand = [];
    for (let i = 0; i < ranks.length; i++) {
        for (let j = 0; j < arrayHandOne.length; j++ ) {
            if (ranks[i] === arrayHandOne[j].charAt(0)) {
                sortedHand.push(arrayHandOne[j])
            }
        }   
    }
        return sortedHand;
    }

    console.log(sorted())

    let sortedHandOne = sorted(arrayHandOne);
    //let sortedHandTwo = sortedHand(arrayHandTwo);
    console.log(sortedHandOne)
    function suitAndRank(sortedHandOne) {
        console.log(sorted)
        for (i = 0; i< sortedHandOne.length; i++) {
            let sted = sortedHandOne;
            rankArray.push(sted[i].charAt(0))
            suitArray.push(sted[i].charAt(1)) 
        } 
    }

    console.log(rankArray, suitArray)

    function countSuites(suitArray) {
        let suitCount = {};
        suitArray.forEach(function(x) { suitCount[x] = (suitCount[x] || 0)+1; });
            return suitCount;
    }

    function countRanks(rankArray) {
        let rankCount = {};
        rankArray.forEach(function(x) { rankCount[x] = (rankCount[x] || 0)+1; });
            return rankCount;
    }

    function isFlush() {
        let cS = countSuites(suitArray);
        if(Object.keys(cS).find(key => cS[key] === 5)) {
            return true;
        } else {
            return false;
        }
    }

    function isStraight() {
        let index = ranks.indexOf(rankArray[0])
        let ref = ranks.slice(index, index + 5).join("")
        let section = rankArray.slice(0).join("")
            if (section === "10JQKA" && section === ref) {
                return "ROYALSTRAIGHT";
            } else if (section === "A2345" || section === ref) {
                return "STRAIGHT"; 
            }else {
                return "FALSE";
            }
    }

    function pairs() {
        let rS = countRanks(rankArray)
        return Object.keys(rS).filter((key) => rS[key] === 2).length
    }


    function whichHand() {
        let rS = countRanks(rankArray)
        if (isFlush() === true && isStraight() === "ROYALSTRAIGHT") {
            console.log('Royal Flush')
        } else if (isFlush() === true && isStraight() === "STRAIGHT") {
            console.log("Straight Flush")
        } else if (Object.keys(rS).find(key => rS[key] === 4)) {
            console.log("Four of a Kind")
        } else if (Object.keys(rS).find(key => rS[key] === 3) && pairs() === 2) {
            console.log("Full House")
        } else if (isFlush === true) {
            console.log("Flush")
        } else if (isStraight === "STRAIGHT") {
            console.log("Straight")
        } else if (Object.keys(rS).find(key => rS[key] === 3)) {
            console.log("Three of a Kind")
        } else if (pairs() === 2) {
            console.log("Two Pair")
        }else if(pairs() === 1) {
            console.log("Pair")
        }else {
            console.log('High Card', rankArray[rankArray.length-1])
        }
    }

    return whichHand();
}

// const hands = ['Royal flush', 'Straight flush', 'Four of a kind', 'Full house',
//     'Flush', 'Straight', 'Three of a kind', 'Two pairs', 'Pair', 'High card']  
//     //compare ranks of hands and return results 

PokerHand();

Solution

  • I've found your problem eventually. The code is changed to give you rankArray and suitArray. There are two issues:

    1. You didn't call the function suitAndRank(sortedHandOne);
    2. There were two issues with your code in the final whichHand() function where you didn't put parenthesis.

    See the completed code below with output:

    function PokerHand() {
      //get ranks of hands
    
      const handOne = "AC 4S 5S 8C AH";
      //const handTwo = ('4S 5S 8C AS AD');
    
      let rankArray = [];
      let suitArray = [];
    
      // let rankArrayTwo = [];
      // let suitArrayTwo = [];
    
      const suits = ["C", "D", "H", "S"];
      const ranks = [
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "J",
        "Q",
        "K",
        "A"
      ];
    
      let arrayHandOne = handOne.split(" ");
      //let arrayHandTwo = handTwo.split(" ");
    
      function sorted() {
        let sortedHand = [];
        for (let i = 0; i < ranks.length; i++) {
          for (let j = 0; j < arrayHandOne.length; j++) {
            if (ranks[i] === arrayHandOne[j].charAt(0)) {
              sortedHand.push(arrayHandOne[j]);
            }
          }
        }
        return sortedHand;
      }
    
      console.log(sorted());
    
      let sortedHandOne = sorted(arrayHandOne);
      //let sortedHandTwo = sortedHand(arrayHandTwo);
      console.log(sortedHandOne);
      function suitAndRank(sortedHandOne) {
        console.log(sorted);
        for (let i = 0; i < sortedHandOne.length; i++) {
          let sted = sortedHandOne;
          rankArray.push(sted[i].charAt(0));
          suitArray.push(sted[i].charAt(1));
        }
      }
    
      suitAndRank(sortedHandOne);
    
      console.log(rankArray, suitArray);
    
      function countSuites(suitArray) {
        let suitCount = {};
        suitArray.forEach(function(x) {
          suitCount[x] = (suitCount[x] || 0) + 1;
        });
        return suitCount;
      }
    
      function countRanks(rankArray) {
        let rankCount = {};
        rankArray.forEach(function(x) {
          rankCount[x] = (rankCount[x] || 0) + 1;
        });
        return rankCount;
      }
    
      function isFlush() {
        let cS = countSuites(suitArray);
        if (Object.keys(cS).find(key => cS[key] === 5)) {
          return true;
        } else {
          return false;
        }
      }
    
      function isStraight() {
        let index = ranks.indexOf(rankArray[0]);
        let ref = ranks.slice(index, index + 5).join("");
        let section = rankArray.slice(0).join("");
        if (section === "10JQKA" && section === ref) {
          return "ROYALSTRAIGHT";
        } else if (section === "A2345" || section === ref) {
          return "STRAIGHT";
        } else {
          return "FALSE";
        }
      }
    
      function pairs() {
        let rS = countRanks(rankArray);
        return Object.keys(rS).filter(key => rS[key] === 2).length;
      }
    
      function whichHand() {
        let rS = countRanks(rankArray);
        if (isFlush() === true && isStraight() === "ROYALSTRAIGHT") {
          console.log("Royal Flush");
        } else if (isFlush() === true && isStraight() === "STRAIGHT") {
          console.log("Straight Flush");
        } else if (Object.keys(rS).find(key => rS[key] === 4)) {
          console.log("Four of a Kind");
        } else if (Object.keys(rS).find(key => rS[key] === 3) && pairs() === 1) {
          console.log("Full House");
        } else if (isFlush() /*First issue*/ === true) {
          console.log("Flush");
        } else if (isStraight() /*Second issue*/ === "STRAIGHT") {
          console.log("Straight");
        } else if (Object.keys(rS).find(key => rS[key] === 3)) {
          console.log("Three of a Kind");
        } else if (pairs() === 2) {
          console.log("Two Pair");
        } else if (pairs() === 1) {
          console.log("Pair");
        } else {
          console.log("High Card", rankArray[rankArray.length - 1]);
        }
      }
    
      return whichHand();
    }
    
    // const hands = ['Royal flush', 'Straight flush', 'Four of a kind', 'Full house',
    //     'Flush', 'Straight', 'Three of a kind', 'Two pairs', 'Pair', 'High card']
    //     //compare ranks of hands and return results
    
    PokerHand();