Advertisement
kstoyanov

05. Nether Realms js fundamentals

Jul 29th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   const data = args
  3.     .shift()
  4.     .split(/[,\s]+/g)
  5.     .sort((a, b) => a.localeCompare(b));
  6.  
  7.   data.forEach((str) => {
  8.     let damage = 0;
  9.  
  10.     const healthR = str.trim().match(/[^\d+\-*/\.]/g);
  11.     const health = healthR.reduce((acc, currV) => acc + currV.charCodeAt(), 0);
  12.  
  13.     const damageR = str.trim().match(/([+-]?[0-9]+\.?[0-9]*)/g);
  14.     if (damageR !== null) {
  15.       damage = damageR !== null && damageR.map(Number).reduce((acc, currV) => acc + currV, 0);
  16.     }
  17.  
  18.     const mathSymbols = str.trim().match(/[\/\*]/g);
  19.     if (mathSymbols !== null) {
  20.       mathSymbols.forEach((symb) => {
  21.         if (symb === '/') {
  22.           damage /= 2;
  23.         } else {
  24.           damage *= 2;
  25.         }
  26.       });
  27.     }
  28.  
  29.  
  30.     console.log(`${str} - ${health} health, ${damage.toFixed(2)} damage`);
  31.   });
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement