# Poker hands I am struggling

Hi guys I am struggling with the following.

``````function pokerHands(arr) {

return true;
}

const testArr = [
'8C TS KC 9H 4S 7D 2S 5D 3S AC',
'5C AD 5D AC 9C 7C 5H 8D TD KS',
'3H 7H 6S KC JS QH TD JC 2D 8S',
'TH 8H 5C QS TC 9H 4D JC KS JS',
'7C 5H KC QH JD AS KH 4C AD 4S'
];

pokerHands(testArr);
``````

I would suggest that you are struggling because you haven’t tried anything.

So, how would I solve this?

Well, the first thing would be to think it through in pseudo-code. For example, if I was writing a chess program and I wanted to write a function to confirm if the current player is check mated, I might do this:

``````loop all enemy pieces
if NOT(one of them attacking the king)
return false
loop all possible moves for current player king
if (any of those moves take the king out of check)
return false
loop all current player's pieces (other than king)
if (they have any moves to block the check) OR (they have any moves to capture the checking piece)
return false
return true
``````

It’s a little naive (it doesn’t account for double check), but you want to do something like that for your poker function. Don’t think about JavaScript, just think about the algorithm. Get that right, then implement in JS.

This is a really important skill for algorithms. For easy algorithms we often do it instinctually, in our heads, but being able to explicitly sit down and think it through. It will become a valuable tool on the job when you have a complex problem, especially if you are working on a team and want to work out how you want to tackle a problem. It also looks good on an interview if you are given an algorithm problem and you take a second to come up with a plan, especially if they can see/hear your process.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.