Advertisement
bebo231312312321

Untitled

Mar 4th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cardGame(input) {
  2.  
  3.     let color = {
  4.         'S': 4,
  5.         'H': 3,
  6.         'D': 2,
  7.         'C': 1,
  8.     }
  9.     let typeCard = {
  10.         "J": 11,
  11.         "Q": 12,
  12.         "K": 13,
  13.         "A": 14,
  14.     }
  15.     let playersResult = {}
  16.     input.forEach(element => {
  17.         [player, hand] = element.split(": ");
  18.         hand = hand.split(", ");
  19.         if (!playersResult.hasOwnProperty(player)) {
  20.             playersResult[player] = []
  21.         }
  22.         playersResult[player].push(...hand)
  23.     });
  24.  
  25.     Object.keys(playersResult)
  26.         .forEach(keys => {
  27.             let cardsHolder = new Set(playersResult[keys])
  28.             let result = 0
  29.             Array.from(cardsHolder)
  30.                 .forEach(hands => {
  31.                     let el = hands.split("")
  32.                     let colorHand = el.pop()
  33.                     let typeHand = el.join("")
  34.                     if (isNaN(typeHand)) {
  35.                         typeHand = typeCard[typeHand]
  36.                     }
  37.                     result += color[colorHand] * Number(typeHand)
  38.                 });
  39.             console.log(`${keys}: ${result}`)
  40.         });
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement