Search code examples
node.jsnumbersblackjackletter

Assign specific number to letter


I'm working on a little text-based Blackjack game in NodeJS. I've got this array:

const ranks = Array('A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K');

The game picks a random number from this array and displays it. But, with letters you can't count. In Blackjack, the "J", "Q" and "K" are 10. The A is 1 OR 11.

I still want it to display to the user the letter, but it has to count with 10. So how can I assign this 10 (or 1/11) to the face-cards, but still display the letter.


Solution

  • The first way that I think of this is to define an array of objects rather than an array of Strings.

    const ranks = [
    {
      actualValue: 11,
      faceValue: 'A'
    },
    {
      actualValue: 2,
      faceValue: '2'
    },
    ...
    ];
    

    With this data structure you can do your math to get counts and also display whatever you want to display.