how to make a deck of cards in javascript

Solutions on MaxInterview for how to make a deck of cards in javascript by the best coders in the world

showing results for - "how to make a deck of cards in javascript"
Travis
03 Feb 2016
1class Deck{
2  constructor(){
3    this.deck = [];
4    this.reset();
5    this.shuffle();
6  }
7
8  reset(){
9    this.deck = [];
10
11    const suits = ['Hearts', 'Spades', 'Clubs', 'Diamonds'];
12    const values = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King'];
13
14    for (let suit in suits) {
15      for (let value in values) {
16        this.deck.push(`${values[value]} of ${suits[suit]}`);
17      }
18    }
19  }
20
21  shuffle(){
22    const { deck } = this;
23    let m = deck.length, i;
24
25    while(m){
26      i = Math.floor(Math.random() * m--);
27
28      [deck[m], deck[i]] = [deck[i], deck[m]];
29    }
30
31    return this;
32  }
33
34  deal(){
35    return this.deck.pop();
36  }
37}
38
39const deck1 = new Deck();
40console.log(deck1.deck);
41deck1.reset();
42console.log(deck1.deck);
43