Give Me a Hand explainer

Aim of the puzzle: Use .shift() to remove the 1st item of an array.
Walk through of solution: The .shift() array method is similar to .pop(), except it removes and returns the 1st element of an array rather than the last.

In this puzzle, a shuffled deck of cards is imported to the puzzle as the array deck. The drawCard() function takes the parameter cards and returns cards.shift(). When this function is called with deck as an argument, it will return the 1st element of deck.

To complete the puzzle, add return cards.shift() inside the drawCard() function body.
Sample code solution:

import { deck } from 'grasshopper.croupier';

function drawCard(cards) {
    return cards.shift();
}

console.log(deck.length);
console.log('Your card is: ' + drawCard(deck));
console.log(deck.length);

Additional Code (hidden code that runs before the puzzle’s code):

const _deck = (()=> {
    const cards = [];
    const suits = ['Hearts', 'Spades', 'Clubs', 'Diamonds'];
    const values = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King'];
    
    for (let suit in suits) {
      for (let value in values) {
        cards.push(`${values[value]} of ${suits[suit]}`);
      }
    }
    return cards;
})();

const shuffle = (array) => {
    for (let i = array.length - 1; i > 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
};

let deck = shuffle(_deck);
2 Likes