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,
},
]