Advertisement
nikolayneykov92

Untitled

Jun 23rd, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function cards () {
  2.   let suits = {
  3.     CLUBS: '\u2663',
  4.     DIAMONDS: '\u2666',
  5.     HEARTS: '\u2665',
  6.     SPADES: '\u2660'
  7.   }
  8.  
  9.   const faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
  10.  
  11.   class Card {
  12.     constructor (face, suit) {
  13.       this.face = face
  14.       this.suit = suit
  15.     }
  16.  
  17.     get face () {
  18.       return this._face
  19.     }
  20.  
  21.     set face (value) {
  22.       if (!faces.includes(value)) {
  23.         throw Error('Invalid face!')
  24.       }
  25.  
  26.       this._face = value
  27.     }
  28.  
  29.     get suit () {
  30.       return this._suit
  31.     }
  32.  
  33.     set suit (value) {
  34.       if (!Object.values(suits).includes(value)) {
  35.         throw Error('Invalid suit!')
  36.       }
  37.  
  38.       this._suit = value
  39.     }
  40.  
  41.     toString () {
  42.       return `${this.face}${this.suit}`
  43.     }
  44.   }
  45.  
  46.   return {
  47.     Suits: suits,
  48.     Faces: faces,
  49.     Card: Card
  50.   }
  51. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement