Advertisement
didkoslawow

Untitled

Mar 19th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function netherRealms(input) {
  2.   const splitter = /\s*,\s*/gm;
  3.   const names = input.split(splitter).sort((a, b) => a.localeCompare(b));
  4.  
  5.   for (const name of names) {
  6.     const deamonHealth = getHealth(name);
  7.     const deamonPower = getAttackPower(name);
  8.     console.log(
  9.       `${name} - ${deamonHealth} health, ${deamonPower.toFixed(2)} damage`
  10.     );
  11.   }
  12.  
  13.   function getHealth(name) {
  14.     const regExp = /[^\d ,\+\-\*\/\.]/gm;
  15.     const deamonName = name.match(regExp).join('');
  16.     let healt = 0;
  17.  
  18.     for (const char of deamonName) {
  19.       const currentCharCode = char.charCodeAt();
  20.       healt += currentCharCode;
  21.     }
  22.     return healt;
  23.   }
  24.  
  25.   function getAttackPower(name) {
  26.     const regExp = /[-+]?\d+(\.\d+)?/gm;
  27.     const isValid = regExp.test(name);
  28.     let sum = 0;
  29.  
  30.     if (!isValid) {
  31.       return 0;
  32.     }
  33.  
  34.     const powerPoints = name.match(regExp);
  35.     powerPoints.forEach((number) => {
  36.       sum += Number(number);
  37.     });
  38.  
  39.     const opeeratorsPattern = /[\*\/]/gm;
  40.     const exeptions = opeeratorsPattern.test(name);
  41.  
  42.     if (!exeptions) {
  43.       return sum;
  44.     }
  45.  
  46.     const matchOperators = name.match(opeeratorsPattern);
  47.  
  48.     matchOperators.forEach((operator) => {
  49.       switch (operator) {
  50.         case '*':
  51.           sum *= 2;
  52.           break;
  53.         case '/':
  54.           sum /= 2;
  55.           break;
  56.       }
  57.     });
  58.  
  59.     return sum;
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement