Liliana797979

viarno reshenie card game - fundamentals

Jul 19th, 2021
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function solve(input) {
  2.  
  3.         let type = {
  4.             '2': 2,
  5.             '3': 3,
  6.             '4': 4,
  7.             '5': 5,
  8.             '6': 6,
  9.             '7': 7,
  10.             '8': 8,
  11.             '9': 9,
  12.             '10': 10,
  13.             'J': 11,
  14.             'Q': 12,
  15.             'K': 13,
  16.             'A': 14
  17.         };
  18.         let power = {
  19.             'S': 4,
  20.             'H': 3,
  21.             'D': 2,
  22.             'C': 1
  23.         };
  24.         let final = {};
  25.         let people = {};
  26.      
  27.         for (const elem of input) {
  28.      
  29.             let [namePlayer, gameCards] = elem.split(': ');
  30.             let cards = gameCards.split(', ');
  31.             if(!people.hasOwnProperty(namePlayer)) {
  32.                 people[namePlayer] = cards.toString();
  33.             } else {
  34.                 let currentValue =  cards +','+ people[namePlayer];
  35.                 people[namePlayer] = currentValue;
  36.             }
  37.         }
  38.      
  39.         for (let elem in people) {
  40.      
  41.             let sum = 0;
  42.             let name = elem;
  43.             let card = people[elem].split(',');
  44.             let set = new Set(card);
  45.             let cardArray = Array.from(set);
  46.      
  47.             for (let card of cardArray) {
  48.                 if (card.length === 2) {
  49.                     let [typeCard, powerCard] = card.split('');
  50.                     sum += Number(type[typeCard]) * Number(power[powerCard]);
  51.                 } else {
  52.                     let [typeCard, other, powerCard] = card.split('');
  53.                     typeCard = typeCard + other;
  54.                     sum += Number(type[typeCard]) * Number(power[powerCard]);
  55.                 }
  56.      
  57.             }
  58.      
  59.             if (!final.hasOwnProperty(name)) {
  60.                 final[name] = sum;
  61.             } else {
  62.                 let currentSum = final[name] + sum;
  63.                 final[name] = currentSum;
  64.             }
  65.      
  66.      
  67.      
  68.      
  69.         }
  70.      
  71.         for (const row in final) {
  72.             console.log(`${row}: ${final[row]}`);
  73.      
  74.         }
  75.      
  76.      
  77.     }
  78.      
  79.      
  80.     solve([
  81.             'Peter: 2C, 4H, 9H, AS, QS',
  82.             'Tomas: 3H, 10S, JC, KD, 5S, 10S',
  83.             'Andrea: QH, QC, QS, QD',
  84.             'Tomas: 6H, 7S, KC, KD, 5S, 10C',
  85.             'Andrea: QH, QC, JS, JD, JC',
  86.             'Peter: JD, JD, JD, JD, JD, JD'
  87.         ]
  88.     );
Advertisement
Add Comment
Please, Sign In to add comment