ErolKZ

Untitled

Nov 23rd, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4. let pattern = /[^0-9\+\-\*\/\.]/;
  5.  
  6. let pattern2 = /(-?\d+\.?\d*)|([\*\/])/g;
  7.  
  8. let workNames = input.split(/[\,][\s*]/);
  9.  
  10. let arr = [];
  11.  
  12.  
  13. workNames = workNames.map(el => el);
  14.  
  15.  
  16. for (let el of workNames) {
  17.  
  18.  
  19. let filteredName = el.split(/[0-9\+\-\*\/\.]/);
  20.  
  21. let arithSymbols = el.split(/[^-?\d+\.?\d*]|\*+/);
  22.  
  23. let multDiv = el.split(/[^\*\/]+/g);
  24.  
  25. multDiv = multDiv.filter(el2 => el2 !== '');
  26.  
  27. multDiv = multDiv.map(el2 => el2.split(''));
  28.  
  29. multDiv = multDiv.flat();
  30.  
  31. arithSymbols = arithSymbols.filter(el2 => el2 !== '');
  32.  
  33. filteredName = filteredName.filter(el2 => el2 !== '');
  34.  
  35. filteredName = filteredName.join('').split('');
  36.  
  37. // console.log(filteredName);
  38.  
  39. let sum = arithSymbols.reduce((acc, el3) => acc + Number(el3), 0);
  40.  
  41. sum = multDiv.reduce((acc, el3) => el3.match(/[*]/) ? acc * 2 : acc / 2, sum);
  42.  
  43. let totalHealth = filteredName.reduce((acc, el2) => acc + el2.charCodeAt(), 0);
  44.  
  45. arr.push([el, { health: totalHealth, damage: sum.toFixed(2) }]);
  46.  
  47.  
  48. }
  49.  
  50.  
  51. arr.sort((a, b) => a[0].localeCompare(b[0]));
  52.  
  53. // console.log(arr);
  54.  
  55. for (let el of arr) {
  56.  
  57. console.log(`${el[0]} - ${el[1]['health']} health, ${el[1]['damage']} damage`);
  58.  
  59. }
  60.  
  61.  
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment