Advertisement
ErolKZ

Untitled

Jan 7th, 2022
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1.  
  2. function solve(inp) {
  3.  
  4.  
  5. function objectCreation(inp) {
  6.  
  7. let demons = {};
  8.  
  9. let arrOfNames = inp.split(/\,\s*/);
  10.  
  11. for (let el of arrOfNames) {
  12.  
  13. demons[el] = { health: getHealth(el), damage: getDamage(el) };
  14.  
  15. }
  16.  
  17. return demons;
  18.  
  19. }
  20.  
  21.  
  22.  
  23.  
  24. function getDamage(par) {
  25.  
  26. let arr = creatingRegExp(par);
  27.  
  28. let damage = arr.reduce((acc, el) => acc + Number(el), 0);
  29.  
  30. let multAndDiv = getMultAndDiv(par);
  31.  
  32. damage = multAndDiv.reduce((acc, el) => el === '*' ? acc * 2 : acc / 2, damage);
  33.  
  34. return damage;
  35.  
  36. }
  37.  
  38.  
  39. function creatingRegExp(par) {
  40.  
  41. let str = par.split(/[^\d\-\+\.]/g);
  42.  
  43. let arr = str.filter(el => el !== '' && el !== '.');
  44.  
  45. return arr;
  46.  
  47. }
  48.  
  49.  
  50.  
  51. function getMultAndDiv(par) {
  52.  
  53. let str = par.split(/[^\*\/]/g);
  54.  
  55. let arr = str.filter(el => el !== '');
  56.  
  57. arr = arr.join('').split('');
  58.  
  59. return arr;
  60.  
  61. }
  62.  
  63.  
  64.  
  65. function getHealth(par) {
  66.  
  67. let str = par.split(/\d|\+|\-|\*|\/|\./g);
  68.  
  69. str = str.filter(el => el !== '');
  70.  
  71. str = str.join('').split('');
  72.  
  73. let health = str.reduce((acc, el) => acc + el.charCodeAt(), 0);
  74.  
  75. return health;
  76.  
  77. }
  78.  
  79.  
  80. function arrCreation() {
  81.  
  82. let demons = objectCreation(inp);
  83.  
  84. let arr = [];
  85.  
  86. for (let demon in demons) {
  87.  
  88. arr.push([demon, demons[demon]]);
  89.  
  90. }
  91.  
  92. return arr;
  93.  
  94. }
  95.  
  96.  
  97. function sorting() {
  98.  
  99. let arr = arrCreation();
  100.  
  101. arr.sort((a, b) => a[0].localeCompare(b[0]));
  102.  
  103. return arr;
  104.  
  105. }
  106.  
  107.  
  108. function printing() {
  109.  
  110. let arr = sorting();
  111.  
  112. for (let el of arr) {
  113.  
  114. console.log(`${el[0]} - ${el[1].health} health, ${el[1].damage.toFixed(2)} damage`);
  115.  
  116. }
  117.  
  118.  
  119. }
  120.  
  121.  
  122. printing();
  123.  
  124. }
  125.  
  126.  
  127. solve(
  128.  
  129. // 'M3ph-0.5s-0.5t0.0**'
  130.  
  131. 'm15*/c-5.044,Az%a.zel'
  132.  
  133. );
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement