Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                /****************************/
/*   Texas hold 'em         */
/****************************/
// 5 Cards in a hand
// Card options:
// Numbers: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A

/** Win conditions
High Card : Highest value card
One Pair : Two cards of equal value
Three of a Kind : Three cards of equal value.
Straight : Cards are consecutive values 
*/


/**
 * Input: Array of player's hands. Each hand consists of 5 cards and will always be valid. You do not need to worry about checking uniqueness of cards or suits
 * 
 * 
 * Output: Index of the winning hand. There will always be a winner, in the case of tied hands, the highest card wins
 * 
 */
const playCameoHoldEm = (playerHands = []) => {
  // your implementation here
  const cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
  const cardValues = cards.reduce((previous, current, index) => {
    previous[current] = index
    return previous
  }, {})
  const winValues = {
    highCard: 0, // Highest value card
    onePair: 1, // Two cards of equal value
    threeOfKind: 2, // Three cards of equal value.
    straight: 3 // Cards are consecutive values 
  }
  const sortHand = (a, b) => {
    if (cardValues[b] < cardValues[a]) {
      return -1;
    }
    if (cardValues[b] > cardValues[a]) {
      return 1;
    }
    return 0;
  }
  let currentWinningIndex = -1;
  let currentHighCardValue = -1;
  let currentWinCondition = -1;

  playerHands.forEach((hand, index) => {
    hand.sort(sortHand).forEach(card => {
      const matchingCards = hand.filter(matchingCard => matchingCard === card).length
      const isStraight = (hand) => {
        return hand.filter((card, index) => cardValues[card] - cardValues[hand[index + 1]] === 1).length === 4;
      }
      // console.log({ hand, currentWinCondition, currentHighCardValue, currentWinningIndex })
      // check for straight
      if (currentWinCondition <= winValues.straight && isStraight(hand)) {
        currentWinCondition = winValues.straight;
        // set higher value straight to winner
        if (cardValues[card] > currentHighCardValue) {
          currentHighCardValue = cardValues[card];
          currentWinningIndex = index;
        }

      // check for three of a kind
      } else if (currentWinCondition <= winValues.threeOfKind && matchingCards === 3) {
        if (currentWinCondition < winValues.threeOfKind || cardValues[card] > currentHighCardValue) {
          currentHighCardValue = cardValues[card];
          currentWinningIndex = index;
        }
        currentWinCondition = winValues.threeOfKind;

      // check for pairs
      } else if (currentWinCondition <= winValues.onePair && matchingCards === 2) {
        if (currentWinCondition < winValues.onePair || cardValues[card] > currentHighCardValue) {
          currentHighCardValue = cardValues[card];
          currentWinningIndex = index;
        }
        currentWinCondition = winValues.onePair;

      // check/set high card
      } else if (currentWinCondition <= winValues.highCard && cardValues[card] > currentHighCardValue) {
        currentHighCardValue = cardValues[card];
        currentWinCondition = winValues.highCard;
        currentWinningIndex = index;
      }
    })
  })
  return currentWinningIndex;
}

/** TEST CASES */
console.log('============')
const assertOrFail = (testCase, input, expected) => { input === expected ? console.log(`[${testCase}] success`) : 
console.log(`[${testCase}] failure: input ${input} does not equal expected ${expected}`);}

// Simple cases
// high card
assertOrFail(
  'High Card',
  playCameoHoldEm([
    ['3', '2', 'J', '7', '4'],
    ['5', '6', '4', '2', '9'],
    ['5', '6', '4', '2', '9'],
    ['5', '6', 'Q', '2', '9'],
    ['5', '6', '4', '2', '9'],
  ]),
3);

// one pair vs high card
assertOrFail(
  'One pair',
  playCameoHoldEm([
    ['2', '2', '10', 'J', '4'],
    ['A', '10', '5', '3', '9'],
    ['A', '10', '5', '3', '3'],
  ]),
2);


// one pair vs one pair
assertOrFail(
  'Pair vs Pair',
  playCameoHoldEm([
    ['2', '2', 'J', '7', '4'],
    ['5', '5', '10', '3', '9'],
    ['3', '3', 'J', 'A', '9']
  ]),
1);


// one pair vs 3 of a kind
assertOrFail(
  'Three of a kind',
  playCameoHoldEm([
    ['2', '2', 'J', '7', '4'],
    ['5', '5', '5', '3', '9'],
    ['2', '2', '2', 'J', '4'],
    ['7', '7', 'Q', 'J', '4'],
    // ['7', '5', 'A', '5', '5'], // this would test equal three of a kind but high card wins
  ]),
1);


// // straight vs pair
assertOrFail(
  'Straight',
  playCameoHoldEm([
    ['2', '3', '6', '5', '4'],
    ['10', '10', '9', 'Q', '8']
  ]),
0);


// 3 hands
assertOrFail(
  'Three Hands',
  playCameoHoldEm([
    ['10', '10', 'Q', 'J', 'A'],
    ['2', '2', '6', '5', '9'],
    ['7', '7', '7', '8', 'Q']
  ]),
2);


              
            
!
999px

Console