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.
Thank you ever so for you blog post. Much thanks again. Adele Ross Don
You can definitely see your skills within the article you write. Valery Murray Ulphia