---
id: 69f90c1329a94b37e2a2086c
title: "Challenge 293: Best Hand"
challengeType: 28
dashedName: challenge-293
---

# --description--

Given an array of five strings representing playing cards, return the name of the best hand.

- Each card is represented as a two-character string: the rank followed by the suit, `"2h"` for example.
  - Ranks, from low to high, are: `"2"`, `"3"`, `"4"`, `"5"`, `"6"`, `"7"`, `"8"`, `"9"`, `"T"`, `"J"`, `"Q"`, `"K"`, and `"A"`.
  - Suits are: `"h"`, `"d"`, `"c"`, and `"s"`.
- Aces (`"A"`) can be used as high or low in a straight.

The hands, in order from worst to best, are:

| Name | Description |
| - | - |
| `"High Card"` | No pair or better |
| `"Pair"` | Two of one rank |
| `"Two Pair"` | Two of one rank and two of another |
| `"Three of a Kind"` | Three of one rank |
| `"Straight"` | Five ranks in a row |
| `"Flush"` | Five of the same suit |
| `"Full House"` | Three of one rank, and two of another |
| `"Four of a Kind"` | Four of one rank |
| `"Straight Flush"` | Five ranks in a row of the same suit |
| `"Royal Flush"` | `"A"`, `"K"`, `"Q"`, `"J"`, `"T"` of the same suit |

Return the name of the best hand.

# --hints--

`getBestHand(["7s", "7h", "7d", "2c", "5h"])` should return `"Three of a Kind"`.

```js
assert.equal(getBestHand(["7s", "7h", "7d", "2c", "5h"]), "Three of a Kind");
```

`getBestHand(["Ks", "Kh", "Kd", "4s", "4h"])` should return `"Full House"`.

```js
assert.equal(getBestHand(["Ks", "Kh", "Kd", "4s", "4h"]), "Full House");
```

`getBestHand(["2h", "5h", "7h", "9h", "Jh"])` should return `"Flush"`.

```js
assert.equal(getBestHand(["2h", "5h", "7h", "9h", "Jh"]), "Flush");
```

`getBestHand(["As", "Ah", "Ad", "Ac", "Kh"])` should return `"Four of a Kind"`.

```js
assert.equal(getBestHand(["As", "Ah", "Ad", "Ac", "Kh"]), "Four of a Kind");
```

`getBestHand(["Ts", "Th", "9d", "9c", "8h"])` should return `"Two Pair"`.

```js
assert.equal(getBestHand(["Ts", "Th", "9d", "9c", "8h"]), "Two Pair");
```

`getBestHand(["9c", "8c", "7c", "6c", "5c"])` should return `"Straight Flush"`.

```js
assert.equal(getBestHand(["9c", "8c", "7c", "6c", "5c"]), "Straight Flush");
```

`getBestHand(["As", "Kh", "Jd", "8c", "5h"])` should return `"High Card"`.

```js
assert.equal(getBestHand(["As", "Kh", "Jd", "8c", "5h"]), "High Card");
```

`getBestHand(["As", "2h", "3d", "4c", "5h"])` should return `"Straight"`.

```js
assert.equal(getBestHand(["As", "2h", "3d", "4c", "5h"]), "Straight");
```

`getBestHand(["Ts", "Th", "7c", "6d", "5h"])` should return `"Pair"`.

```js
assert.equal(getBestHand(["Ts", "Th", "7c", "6d", "5h"]), "Pair");
```

`getBestHand(["As", "Ks", "Qs", "Js", "Ts"])` should return `"Royal Flush"`.

```js
assert.equal(getBestHand(["As", "Ks", "Qs", "Js", "Ts"]), "Royal Flush");
```

# --seed--

## --seed-contents--

```js
function getBestHand(cards) {

  return cards;
}
```

# --solutions--

```js
function getBestHand(cards) {
  const rankOrder = "23456789TJQKA";

  const ranks = cards.map(c => c[0]);
  const suits = cards.map(c => c[1]);

  const isFlush = suits.every(s => s === suits[0]);

  const rankIndices = ranks.map(r => rankOrder.indexOf(r)).sort((a, b) => a - b);

  function isStraight(indices) {
    const isRegular = indices.every((r, i) => i === 0 || r === indices[i - 1] + 1);
    const isLowAce = JSON.stringify(indices) === JSON.stringify([0, 1, 2, 3, 12]);
    return isRegular || isLowAce;
  }

  const straight = isStraight(rankIndices);

  const counts = {};
  ranks.forEach(r => counts[r] = (counts[r] || 0) + 1);
  const freq = Object.values(counts).sort((a, b) => b - a);

  if (isFlush && JSON.stringify(rankIndices) === JSON.stringify([8, 9, 10, 11, 12])) return "Royal Flush";
  if (isFlush && straight) return "Straight Flush";
  if (freq[0] === 4) return "Four of a Kind";
  if (freq[0] === 3 && freq[1] === 2) return "Full House";
  if (isFlush) return "Flush";
  if (straight) return "Straight";
  if (freq[0] === 3) return "Three of a Kind";
  if (freq[0] === 2 && freq[1] === 2) return "Two Pair";
  if (freq[0] === 2) return "Pair";
  return "High Card";
}
```
