Pijomir

Nether Realms

Nov 20th, 2023 (edited)
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function printDemonsStats(input) {
  2.     let demons = input.split(/[, ]+/);
  3.     let demonsStats = {};
  4.     let healthPattern = /[^0-9+\-*\/.]/g;
  5.     let damagePattern = /\-?\+?\d+\.?\d*/g;
  6.     let someSymbolsPattern = /[\*\/]/g;
  7.     demons.forEach(demon => {
  8.         let healthParams = demon.match(healthPattern);
  9.         let health = 0;
  10.         healthParams.forEach(param => {
  11.             let healthPart = param.charCodeAt();
  12.             health += healthPart;
  13.         });
  14.         let damageParams = demon.match(damagePattern);
  15.         let damage = 0;
  16.         if (damageParams) {
  17.             let damageSum = damageParams.map(Number).reduce((a, b) => a + b, 0);
  18.             let symbols = demon.match(someSymbolsPattern);
  19.             if (symbols) {
  20.                 symbols.forEach(symbol => {
  21.                     if (symbol === '*') {
  22.                         damageSum *= 2;
  23.                     } else {
  24.                         damageSum /= 2;
  25.                     }
  26.                 });
  27.             }
  28.  
  29.             damage = damageSum;
  30.         }
  31.  
  32.         demonsStats[demon] = {};
  33.         demonsStats[demon].health = health;
  34.         demonsStats[demon].damage = damage;
  35.     });
  36.     let sortedDemonsStats = Object.entries(demonsStats).sort((a, b) => a[0].localeCompare(b[0]));
  37.     sortedDemonsStats.forEach(demon => {
  38.         console.log(`${demon[0]} - ${demon[1].health} health, ${demon[1].damage.toFixed(2)} damage`);
  39.     });
  40. }
Advertisement
Add Comment
Please, Sign In to add comment