JAVASCRIPT   14

Nether realms

Guest on 24th November 2021 08:01:00 PM

  1. function solve(input) {
  2.     let deamonsStorage = {};
  3.     let deamonName = /[^\,\ ]{1,}/g
  4.     let deamons = (input.match(deamonName));
  5.     let damageRegex = /[|-]?[\d]+(?:\.[\d]+)?/g;
  6.     let healthRegex = /[^0-9\*\/\-\+\.]/g;
  7.     let multiply = /[*\/]/g;
  8.     for (let element of deamons) {
  9.  
  10.         if (!element.includes(",") && !element.includes(" ")) {
  11.             let deamonHealt = 0;
  12.             let deamonDamage = 0;
  13.             let validMatch = element.match(healthRegex);
  14.             validMatch.forEach(element => {
  15.                 let current = element.codePointAt(0);
  16.                 deamonHealt += current;
  17.                 return deamonHealt;
  18.             });
  19.             //console.log(deamonHealt);
  20.             let damageMatch = element.match(damageRegex);
  21.             if (damageMatch) {
  22.                 damageMatch.forEach(element => {
  23.                     deamonDamage += Number(element);
  24.                     return deamonDamage;
  25.                 });
  26.             }
  27.             let multiplyMatch = element.match(multiply);
  28.             if (multiplyMatch) {
  29.                 for (let el of multiplyMatch) {
  30.                     if (el === "*") {
  31.                         deamonDamage *= 2;
  32.                     } else if (el === "/") {
  33.                         deamonDamage /= 2;
  34.                     }
  35.  
  36.                 }
  37.             }
  38.             //console.log(deamonDamage)
  39.             deamonsStorage[element] = {};
  40.             deamonsStorage[element]['healt'] = deamonHealt;
  41.             deamonsStorage[element]['damage'] = deamonDamage;
  42.         }
  43.  
  44.     }
  45.  
  46.     let keys = Object.keys(deamonsStorage).sort((a, b) => a.localeCompare(b))
  47.  
  48.     for (let el of keys) {
  49.         let arr = Object.entries(deamonsStorage[el]);
  50.         let output = [];
  51.  
  52.         output.push(`${(arr[0][1])} health, ${(arr[1][1]).toFixed(2)} damage `)
  53.  
  54.         console.log(`${el} - ${output.join(" ")}`)
  55.     }
  56.  
  57. }

Raw Paste


Login or Register to edit or fork this paste. It's free.