Advertisement
TZinovieva

Playing Cards JS Advanced

Oct 4th, 2023
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createCard(face, suit) {
  2.     // Define valid faces and suits
  3.     const validFaces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
  4.     const validSuits = ['S', 'H', 'D', 'C'];
  5.  
  6.     // Check if the provided face and suit are valid
  7.     if (validFaces.includes(face) && validSuits.includes(suit)) {
  8.         const card = {
  9.             face,
  10.             suit,
  11.             toString() {
  12.                 // Use Unicode symbols for suits
  13.                 const suitSymbol = {
  14.                     'S': '\u2660', // Spades
  15.                     'H': '\u2665', // Hearts
  16.                     'D': '\u2666', // Diamonds
  17.                     'C': '\u2663'  // Clubs
  18.                 };
  19.    
  20.                 return `${this.face}${suitSymbol[this.suit]}`;
  21.             }
  22.         };
  23.        
  24.         return card;
  25.     } else {
  26.         throw new Error("Error");
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement