Andrew Kim
Andrew Kim's Blog

Andrew Kim's Blog

Using Iterator To Generate A Suite of Card Decks

Andrew Kim's photo
Andrew Kim
·Jul 30, 2022·

1 min read

Using Iterator To Generate A Suite of Card Decks

const cardDeck = {
  suits: ['♧', '♢', '♥', '♤'],
  court: ['J', 'Q', 'K', 'A'],
  [Symbol.iterator]: function* () {
    for (let suit of this.suits) {
      for (let i = 2; i <= 10; i++) yield suit + i;
      for (let c of this.court) yield suit + c;
    }
  }
}

console.log(...cardDeck)

Credit: observablehq.com/@anjana/the-power-of-js-ge..

 
Share this