Javascript object and array destructuring

Viewed 28

I'm working on a problem with instructions to create functions that manage a deck of cards and pre-written unit tests. But I don't understand why my code is failing the unit test and what the test really wants the function to be.

Here is the instruction for the function deal. Parameters:

cardsArray - an Array of card objects numHands (optional, default value: 2) - the number of hands of cards to create cardsPerHand (optional, default value: 5) - the number of cards per hand Returns:

An object consisting of two properties, both with an Array as a vlue:

deck: a new Array consisting of the cards in cardArray with the last numHands * numcardsPerHand removed hands a new Array consisting of numHands sub arrays of card objects For example, dealing 2 hands with 2 cards per hand might result in this object:

{
    deck: [ (copy of original array of cards with last 4 cards removed) ]
    hands: [
        [{suit: '❤️', rank: '3'}, {suit: '❤️', rank: '4'}],
        [{suit: '❤️', rank: '5'}, {suit: '❤️', rank: '6'}]
    ]
}

Here is my code, which I wrote according to the instruction.

export function deal(cardsArray, numHands = 2, cardsPerHand = 5) {
    console.log(numHands, cardsPerHand);
    const copy = cardsArray.slice(0);
    const numPick = cardsPerHand * numHands;
    const allCards = copy.splice(cardsArray.length-numPick, numPick);
    const hands = [allCards.slice(0, cardsPerHand), allCards.slice(cardsPerHand)];
    return [copy, hands];
}

Here is the unit test

    it('defaults to two hands, each with five cards', () => {
      const numHands = 2;
      const cardsPerHand = 5;
      const originalDeck = cards.generateDeck();
      const { hands } = cards.deal(originalDeck);
      const [hand1, hand2] = hands;
      console.log(hand1, hand2);
      expect(hands.length).to.equal(numHands);
      expect(hand1.length).to.equal(cardsPerHand);
      expect(hand2.length).to.equal(cardsPerHand);
    });

the generated deck looks like this(with self defined card objects):

[
  cards { suit: '❤️', rank: 'A' },
  cards { suit: '❤️', rank: '2' },
  cards { suit: '❤️', rank: '3' },
  cards { suit: '❤️', rank: '4' },
  cards { suit: '❤️', rank: '5' },
  cards { suit: '❤️', rank: '6' },
  cards { suit: '❤️', rank: '7' },
  cards { suit: '❤️', rank: '8' },
  cards { suit: '❤️', rank: '9' },
  cards { suit: '❤️', rank: '10' },
  cards { suit: '❤️', rank: 'J' },
  cards { suit: '❤️', rank: 'Q' },
  cards { suit: '❤️', rank: 'K' },
  cards { suit: '♣️', rank: 'A' },
  cards { suit: '♣️', rank: '2' },
  cards { suit: '♣️', rank: '3' },
  cards { suit: '♣️', rank: '4' },
  cards { suit: '♣️', rank: '5' },
  cards { suit: '♣️', rank: '6' },
  cards { suit: '♣️', rank: '7' },
  cards { suit: '♣️', rank: '8' },
  cards { suit: '♣️', rank: '9' },
  cards { suit: '♣️', rank: '10' },
  cards { suit: '♣️', rank: 'J' },
  cards { suit: '♣️', rank: 'Q' },
  cards { suit: '♣️', rank: 'K' },
  cards { suit: '♦️', rank: 'A' },
  cards { suit: '♦️', rank: '2' },
  cards { suit: '♦️', rank: '3' },
  cards { suit: '♦️', rank: '4' },
  cards { suit: '♦️', rank: '5' },
  cards { suit: '♦️', rank: '6' },
  cards { suit: '♦️', rank: '7' },
  cards { suit: '♦️', rank: '8' },
  cards { suit: '♦️', rank: '9' },
  cards { suit: '♦️', rank: '10' },
  cards { suit: '♦️', rank: 'J' },
  cards { suit: '♦️', rank: 'Q' },
  cards { suit: '♦️', rank: 'K' },
  cards { suit: '♠️', rank: 'A' },
  cards { suit: '♠️', rank: '2' },
  cards { suit: '♠️', rank: '3' },
  cards { suit: '♠️', rank: '4' },
  cards { suit: '♠️', rank: '5' },
  cards { suit: '♠️', rank: '6' },
  cards { suit: '♠️', rank: '7' },
  cards { suit: '♠️', rank: '8' },
  cards { suit: '♠️', rank: '9' },
  cards { suit: '♠️', rank: '10' },
  cards { suit: '♠️', rank: 'J' },
  cards { suit: '♠️', rank: 'Q' },
  cards { suit: '♠️', rank: 'K' }
]

I think it is the destructuring part where hands end up undefined.

hands = {cards.deal(originalDeck)}

However, I don't know how I should modify the function so that it will pass the test?

Edit: Currently, the test is failing due to TypeError:

TypeError: hands is not iterable
1 Answers

Arrays don't have property named hands

Use this isntead:

const [hand1, hand2] = cards.deal(originalDeck);
Related