Advertisement
ErolKZ

Untitled

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