简体   繁体   English

Javascript object 和数组解构

[英]Javascript object and array destructuring

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.但我不明白为什么我的代码没有通过单元测试以及测试真正想要 function 是什么。

Here is the instruction for the function deal.这是 function 交易的说明。 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: cardArray - 卡片对象数组 numHands (可选,默认值:2) - 要创建的卡片手数 cardsPerHand (可选,默认值:5) - 每手卡片数量 返回:

An object consisting of two properties, both with an Array as a vlue:一个 object 由两个属性组成,两个属性都以 Array 作为 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:牌组:一个新的数组,由 cardArray 中的牌组成,最后一个 numHands * numcardsPerHand 被移除了一个新的数组,由 numHands 个子牌对象组成的 arrays 例如,用每手 2 张牌处理 2 手牌可能会导致这个 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?但是,我不知道应该如何修改 function 才能通过测试?

Edit: Currently, the test is failing due to TypeError:编辑:目前,由于 TypeError,测试失败:

TypeError: hands is not iterable

Arrays don't have property named hands Arrays 没有名为hands的属性

Use this isntead:使用这个而不是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM