Advertisement
ilianrusev

Untitled

Jan 26th, 2022
20
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function deckOfCards(arr) {
  2. let result = []
  3.  
  4. for (let card of arr) {
  5. let face = card.slice(0, -1);
  6. let suit = card.slice(-1);
  7. try {
  8. result.push(createCard(face, suit))
  9. } catch (err) {
  10. console.log(`Invalid card: ${card}`);
  11. return;
  12. }
  13. }
  14. console.log(result.join(' '));
  15.  
  16. function createCard(face, suit) {
  17. let faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
  18.  
  19. let suits = {
  20. "S": '\u2660',
  21. "H": '\u2665',
  22. "D": '\u2666',
  23. "C": '\u2663'
  24. }
  25. if (faces.includes(face) == false) {
  26. throw new Error(`Invalid Card: ${face}`)
  27. }
  28. if (Object.keys(suits).includes(suit) == false) {
  29. throw new Error(`Invalid Card: ${suit}`)
  30. }
  31. return {
  32. face,
  33. suit: suits[suit],
  34. toString() {
  35. return this.face + this.suit ;
  36.  
  37. }
  38. }
  39.  
  40. }
  41. }
Advertisement
RAW Paste Data Copied
Advertisement