1

Trying to build a video poker app and I've got most of the winning logic done but I can not wrap my head around two pairs.

function isTwoPair() {
  const tempHand = [...playerHand];
  let reduceHand = tempHand.reduce((acc, curVal) => {
    if (curVal.rank in acc) {
      acc[curVal.rank]++;
    } else {
      acc[curVal.rank] = 1;
    }
    return acc;
  }, {});
  const sortedHand = Object.fromEntries(Object.entries(reduceHand).sort());
  for (const [key, value] of Object.entries(reduceHand)) {
    let pairs = 0;
    if (value === 2) {
      pairs++;
      if (pairs === 2) {
        return true;
      }
    }
  }
}

My thought was to use reduce to determine the number of values each key has then sort it (ascending) and loop through it. If any value was equal to 2 then it would update the pairs variable by 1. Once pairs got to 2 then it would return true.

What is a better way, or the correct way I should say since this doesnt work, of finding two pairs in a given array of objects.

The deck is an array of objects that look like this:

[
 {
    card: "Ah",
    rank: 14,
    suit: "hearts",
    img: "./images/hearts/hearts-A.svg",
    isHold: false,
 },
]
Mundy
  • 19
  • 5
  • Your function should take arguements of the hand, instead of referencing outside the function, unless you can expose more for that specific class you implements. – Fallenreaper Jan 27 '23 at 15:25

3 Answers3

0
function isTwoPair() {
  const reduceHand = reduceHandRank();
  let pair = 0;
  for (const [key, value] of Object.entries(reduceHand)) {
    if (value === 2) {
      pair++;
    }
  }
  if (pair === 2) {
    return true;
  }
}

Determination will get you there! Hot dawg!!!

Mundy
  • 19
  • 5
0

I'd count each card rank. If the count is more than 2, return false. After counting, filter the values that are 2 and return if its length is equal to 2

function isTwoPair() {
  let count={}
  for(let i=0;i<playerHand.length;i++){
    let card=playerHand[i]
    if(card.rank in count){
      count[card.rank]+=1
      if(count[card.rank]>2){
        return false
      }
    }else{
      count[card.rank]=1
    }
  }
  return Object.values(count).filter(c=>c==2).length == 2
}

Full example:

let playerHand = [{
    rank: 14,
    suit: "spades",
    img: "./images/hearts/hearts-A.svg",
    isHold: false,
  },
  {
    rank: 14,
    suit: "hearts",
    img: "./images/hearts/hearts-A.svg",
    isHold: false,
  },
  {
    rank: 14,
    suit: "diamonds",
    img: "./images/hearts/diamonds-A.svg",
    isHold: false,
  },
  {
    rank: 14,
    suit: "clubs",
    img: "./images/hearts/diamonds-A.svg",
    isHold: false,
  },
  {
    rank: 8,
    suit: "hearts",
    img: "./images/hearts/hearts-8.svg",
    isHold: false,
  },
]

function isTwoPair() {
  let count={}
  for(let i=0;i<playerHand.length;i++){
    let card=playerHand[i]
    if(card.rank in count){
      count[card.rank]+=1
      if(count[card.rank]>2){
        return false
      }
    }else{
      count[card.rank]=1
    }
  }
  return Object.values(count).filter(c=>c==2).length == 2
}

console.log(isTwoPair())
playerHand = [{
    rank: 14,
    suit: "hearts",
    img: "./images/hearts/hearts-A.svg",
    isHold: false,
  },
  {
    rank: 14,
    suit: "spades",
    img: "./images/hearts/hearts-A.svg",
    isHold: false,
  },
  {
    rank: 13,
    suit: "clubs",
    img: "./images/hearts/diamonds-A.svg",
    isHold: false,
  },
  {
    rank: 13,
    suit: "diamonds",
    img: "./images/hearts/diamonds-A.svg",
    isHold: false,
  },
  {
    rank: 8,
    suit: "hearts",
    img: "./images/hearts/hearts-8.svg",
    isHold: false,
  },
]
console.log(isTwoPair())
depperm
  • 10,606
  • 4
  • 43
  • 67
0

I would recommend passing in playerHand so your function can be used for multiple hands. If you're not familiar, ~~ has a side-effect of turning non-truthy values into 0, and leaving integers intact.

function isTwoPair(playerHand) {
  let counts={};
  playerHand.forEach(hand => counts[hand.rank] = ~~counts[hand.rank]+1);
  let sorted=Object.values(counts).sort();
  return sorted[sorted.length-1]==2 && sorted[sorted.length-2]==2
}
phatfingers
  • 9,770
  • 3
  • 30
  • 44