Advertisement
divanov94

Untitled

Oct 29th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Card {
  2.     constructor(face,suit){
  3.         this.face=face;
  4.         this.suit=suit;
  5.     }
  6.  
  7.     get face(){
  8.         return this.face;
  9.     }
  10.     set face(valueFace){
  11.         let valid=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
  12.         if(!valid.includes(valueFace)){
  13.             throw new Error('Invalid card face:' +valueFace);
  14.         }
  15.         this.face=valueFace;
  16.     }
  17.     get suit(){
  18.         return this.suit;
  19.     }
  20.     set suit(valueSuit){
  21.         let validSuit=['C','D','H','S'];
  22.         if(!validSuit.includes(valueSuit)){
  23.             throw new Error('Invalid card suit:' +valueSuit)
  24.         }
  25.         this.suit=valueSuit;
  26.     }
  27.  
  28.     toString(){
  29.         let convertObj={
  30.             'C': "\u2663",
  31.             'D': "\u2666",
  32.             'H': "\u2665",
  33.             'S': "\u2660",
  34.         }
  35.         return this.face+convertObj[this.suit];
  36.     }
  37. }
  38. let card=new Card('J','D');
  39. console.log(card);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement