Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function cardGame(input) {
- let color = {
- 'S': 4,
- 'H': 3,
- 'D': 2,
- 'C': 1,
- }
- let typeCard = {
- "J": 11,
- "Q": 12,
- "K": 13,
- "A": 14,
- }
- let playersResult = {}
- input.forEach(element => {
- [player, hand] = element.split(": ");
- hand = hand.split(", ");
- if (!playersResult.hasOwnProperty(player)) {
- playersResult[player] = []
- }
- playersResult[player].push(...hand)
- });
- Object.keys(playersResult)
- .forEach(keys => {
- let cardsHolder = new Set(playersResult[keys])
- let result = 0
- Array.from(cardsHolder)
- .forEach(hands => {
- let el = hands.split("")
- let colorHand = el.pop()
- let typeHand = el.join("")
- if (isNaN(typeHand)) {
- typeHand = typeCard[typeHand]
- }
- result += color[colorHand] * Number(typeHand)
- });
- console.log(`${keys}: ${result}`)
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement