ErolKZ

Untitled

Feb 9th, 2022
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. function printDeckOfCards(cards) {
  2.  
  3.  
  4. function createCard(face, suit, card1) {
  5.  
  6. let validFaces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
  7.  
  8. let validSuits = {
  9.  
  10. S: '♠',
  11.  
  12. H: '♥',
  13.  
  14. D: '♦',
  15.  
  16. C: '♣'
  17.  
  18. };
  19.  
  20.  
  21. if (!validFaces.includes(face) || validSuits[suit] === undefined) {
  22.  
  23. throw new Error(`Invalid card: ${card1}`);
  24.  
  25. }
  26.  
  27.  
  28. let card = {
  29.  
  30. face: face,
  31.  
  32. suit: validSuits[suit],
  33.  
  34. toString() {
  35.  
  36. return this.face + this.suit;
  37.  
  38. }
  39.  
  40. };
  41.  
  42. return card.toString();
  43.  
  44.  
  45. }
  46.  
  47. let output = '';
  48.  
  49. for (let el of cards) {
  50.  
  51. let cur = el.split('');
  52.  
  53. let face = undefined;
  54.  
  55. let suit = undefined;
  56.  
  57. let card1 = el;
  58.  
  59.  
  60. if (!isNaN(cur[0])) {
  61.  
  62. face = cur.filter(el => !isNaN(el));
  63.  
  64. suit = cur.filter(el => isNaN(el));
  65.  
  66. face = face.join('');
  67.  
  68. suit = suit.join('');
  69.  
  70. } else {
  71.  
  72. face = cur[0];
  73.  
  74. suit = cur[1];
  75.  
  76. }
  77.  
  78. output += `${createCard(face, suit, card1)} `;
  79.  
  80.  
  81. }
  82.  
  83. console.log(output);
  84.  
  85. return output;
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment