Skip to content

A7 - Arrays and Full Poker Hands

Arrays in JavaScript are like lists in Python. Here's a couple examples of working with them.

let myArray = [6, 3, 9, 4, 7]

console.log(myArray[2]) // prints 9

for (let i = 0; i < 5; i++) {
    console.log("Item number "+i+" is "+myArray[i])
}

for (let i = 0; i < 4; i++) {
    let difference =  myArray[i+1] - myArray[i]
    console.log("Difference between item "+i+" and its neighbour to the right is "+difference)
}

In our five card Poker scenario, we will be using an array of Card objects. You can use syntax like this:

// This would total the strength of the cards in the hand
// (not that you would ever need to do this in Poker!)

let totalStrength = 0
for (let i = 0; i < 5; i++) {
    totalStrength += hand[i].strength
}

Implementing Full Poker Hands

You can use the following chart for reference, and see details here.

The high card and pair algorithms are already implemented for you. Take some time to get a feel for how they were, including the alternate algorithm I included in comments for the pair. Then go ahead and try to implement algorithms for the other functions that have been started for you.

Suggestion

If you want to do this from easiest to hardest, I would suggest going in this order:

  1. Triple
  2. Flush
  3. Straight
  4. Two pair

As an extension, you could try to implement some of the other hand types.