Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69f90c1329a94b37e2a2086c.md
T
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

4.0 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69f90c1329a94b37e2a2086c Challenge 293: Best Hand 28 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".

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

--seed--

--seed-contents--

function getBestHand(cards) {

  return cards;
}

--solutions--

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";
}