Advertisement
Lulunga

05. Nether Realms regex

Jul 26th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let participants = {};
  3.  
  4.     let demonNames = arr.join().split(/[,][\s]*/).filter(e => e).map(e => e.trim()).filter(el => !el.includes(','));
  5.  
  6.     demonNames.forEach(line => {
  7.         participants[line] = {};
  8.         let health = line.match(/[^0123456789\+\-\.\*\/]/gm).map(e => e.charCodeAt(0)).reduce((a, b) => a + b, 0);
  9.  
  10.         let damage = line.match(/[+-]?[0-9]+[.]?[0-9]*/gm);
  11.  
  12.         if (!damage) {
  13.             damage = 0.00;
  14.         } else {
  15.             damage = damage.map(Number).reduce((a, b) => a + b, 0);
  16.         }
  17.  
  18.         let multiplyDivide = line.match(/[*\/]/gm);
  19.         if (multiplyDivide) {
  20.             multiplyDivide.forEach((symbol) => {
  21.  
  22.                 switch (symbol) {
  23.                     case '*':
  24.                         damage *= 2;
  25.                         break;
  26.                     case '/':
  27.                         damage /= 2;
  28.                         break;
  29.                 }
  30.             });
  31.         }
  32.         participants[line]['health'] = health;
  33.         participants[line]['damage'] = damage.toFixed(2);
  34.  
  35.  
  36.     });
  37.     let result = Object.keys(participants)
  38.         .sort()
  39.         .forEach(key => {
  40.             console.log(`${key} - ${participants[key].health} health, ${participants[key].damage} damage`);
  41.         });
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement