How can I give a string a numerical value?

Hi all,

I want to create a program which uses a deck of cards. So far the deck object uses properties which correlate to the 4 suits. And each suit contains arrays with string elements that correlate to the 13 cards of that suit:

const deck = {
       _cards: {
            clubs: ['2C', '3C', '4C' ... 'QC', 'KC', 'AC'],

I know that if I use the parseInt() function with the radix of 10 I can give the 2 of clubs element (2C), for example, the value of 2. But the problem I run into is when I want to give a numerical value to the elements that depict the face cards and the aces. Eventually I want code some logic that will compare the hand of cards based on value. Does anyone know how I can solve this problem?

Thank you

Hello, @brianawotwi.

I might consider a slightly different approach. Is there some advantage to having an array of clubs? If so, that’s fine, but in that case do you need the 'C' to denote the 2 of clubs?

Another idea (what I would do) would be to have each card be an object with three properties. An id or face value or whatever you want to call it, a suit and a value. For example:

const ac = {
  faceValue: "Ace",
  suit: "clubs",
  value: 11 // or 1 that's another potential challenge depending on your end game

Hi @midlindner

So my aim is to randomly generate the computer’s hand which will be it’s own array by pushing 5 unique elements from available suit of cards. And each element cannot be what is in the player’s hand which will also be it’s own array.

It will just be a simple game of poker. I am thinking of using some short-hand notation to tell what cards the user and the computer each hold. It will log to the console something like:

Your hand holds: 2H, 5C, JD, JS, KD
The computer holds: QH, 7D, QS, QD, 5H
The computer wins with a 3 Of A Kind

By seeing your code example I can already see where I can make some tweaks.

1 Like

Sounds fun. A few other things to consider. You could utilize a factory function or class or a few of each for cards and the deck rather than creating 52 individual objects manually.

1 Like

Yo! I finally came around to finishing that poker project. Check it out here:

1 Like