Javascript exercise of random cards

An exercise of javascript random cards
Difficulty

It happened to me once to complete an interesting and curious javascript exercise of random cards.

The request was as follows:

” Having a deck of French cards (consisting of 52 cards), 10 cards must be drawn

(using the Math.random function).

Of those decide the suit and number of each card drawn.

Also, if there are duplicate cards, re-draw them until 10 different cards are found. “

In this case we talked about cards but this was just an excuse to put some javascript features into practice. It’s also clear what I did could have been done in a thousand different ways. It’s up to you to take up the challenge and make an alternative implementation, with fewer lines of code or more elegant.

First I establish which are figures and which are not and define the functions to determine the suit of the card.

let randomCards = [];
const range = 13;
let rn = n => n * range; // Get range sector
let between = (n, min) => n > min && n <= min + range; // Get range
let figures = { 1: 'ace', 11: 'infantryman', 12: 'queen', 13: 'king' }; // Figure cards
let isFigure = n => (figures.hasOwnProperty(n) ? figures[n] : n); // Get (if) figure
let suits = [
  // Suits array
  { name: 'hearts', symbol: '♥️', is: n => n <= rn(1) },
  { name: 'diamonds', symbol: '♦', is: n => between(n, rn(1)) },
  { name: 'clubs', symbol: '♣', is: n => between(n, rn(2)) },
  { name: 'spades', symbol: '♠', is: n => between(n, rn(3)) }
];
let getCardNumber = n => isFigure(n - (n - (n % range)) + 1); // Extract the card number/figure


Once the rules and limits of my set have been established, I proceed to the extraction.
With a possible recursion on the getRandomCard() function, I repeat the extraction operation until the extracted card is new. For didactic reasons I write the error in the console, to know which card has already been drawn.

function getRandomCard() {  
  // Random number 0 <= n <= 51
  let randomCard = Math.round(Math.random() * 51);
  if (randomCards.find(v => v === randomCard)) {
    // Search if duplicated card
    console.error(new Error('Card ' + randomCard + ' already extracted!'));
    return getRandomCard(); // Recursive random card on duplicated card
  }
  randomCards.push(randomCard); // Add to extracted cards
// ...to be continued.
}


Loop over the suits of the cards and determine which card suit is, which number is and if is a figure, then return the string with the output:

function getRandomCard() {  
// ...
  for(let suit of suits) { // Loop until found 
    if (suit.is(randomCard) && randomCard < 52) {
      // Is a known suit?
      // Return random card informations
      return  `Suit ${suit.symbol} (${suit.name}).` + 
              ` Random number: ${randomCard}` + 
              ` Card number: ${getCardNumber(randomCard)}`;
    }
  }
}

To draw a random card, just call the following function:

getRandomCard();


My full development of javascript random cards was the following:

let randomCards = [];
const range = 13;
let rn = n => n * range; // Get range sector
let between = (n, min) => n > min && n <= min + range; // Get range
let figures = { 1: 'ace', 11: 'infantryman', 12: 'queen', 13: 'king' }; // Figure cards
let isFigure = n => (figures.hasOwnProperty(n) ? figures[n] : n); // Get (if) figure
let suits = [
  // Suits array
  { name: 'hearts', symbol: '♥️', is: n => n <= rn(1) },
  { name: 'diamonds', symbol: '♦', is: n => between(n, rn(1)) },
  { name: 'clubs', symbol: '♣', is: n => between(n, rn(2)) },
  { name: 'spades', symbol: '♠', is: n => between(n, rn(3)) }
];
let getCardNumber = n => isFigure(n - (n - (n % range)) + 1); // Extract the card number/figure
function getRandomCard() {
  // Random number 0 <= n <= 51
  let randomCard = Math.round(Math.random() * 51);
  if (randomCards.find(v => v === randomCard)) {
    // Search if duplicated card
    console.error(new Error('Card ' + randomCard + ' already extracted!'));
    return getRandomCard(); // Recursive random card on duplicated card
  }
  randomCards.push(randomCard); // Add to extracted cards

  for(let suit of suits) { // Loop until found 
    if (suit.is(randomCard) && randomCard < 52) {
      // Is a known suit?
      // Return random card informations
      return  `Suit ${suit.symbol} (${suit.name}).` + 
              ` Random number: ${randomCard}` + 
              ` Card number: ${getCardNumber(randomCard)}`;
    }
  }
}

for(let i = 0; i < 10; i++) {
  console.log(getRandomCard());
}


Here’s a working widget:


That’s all for this javascript exercise of random cards.
See you next time.

1
1 person likes this.
Please wait...

2 thoughts on...
  1. You can definitely see your skills within the article you write. Valery Murray Ulphia

Leave a Reply

Thanks for choosing to leave a comment.
Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published.
Please do NOT use keywords in the name field. Let's have a personal and meaningful conversation.