Advertisement
Guest User

05. Nether Realms

a guest
Jul 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function netherRealms(input) {
  2.     let demons = {};
  3.     input = String(input[0]).split(',').map(demon => demon.trim());
  4.     let patternHealth = /[^0-9+\-*/.]/g;
  5.     let patternDamage = /[-\|+]?[0-9]+[.]?[0-9]*/g;
  6.  
  7.     for (let demon of input) {
  8.         let health = 0;
  9.         let damage = 0;
  10.         if (demon.match(patternHealth)) {
  11.             health = demon.match(patternHealth).map(x => x.charCodeAt()).reduce((a, b) => a + b, 0);
  12.         }
  13.         let multiplication = demon.split('').filter(x => (x === '*' || x === '/'));
  14.        
  15.         if (demon.match(patternDamage)) {
  16.             damage = demon.match(patternDamage).map(Number).reduce((a, b) => a + b, 0);
  17.         }
  18.         multiplication.forEach(element => (element === '*') ? damage *= 2 : damage /= 2);
  19.  
  20.         demons[demon] = {
  21.             health,
  22.             damage
  23.         }
  24.  
  25.     }
  26.     Object.entries(demons)
  27.         .sort((a, b) => a[0].localeCompare(b[0]))
  28.         .forEach(demon => {
  29.             console.log(`${demon[0]} - ${demon[1].health} health, ${demon[1].damage.toFixed(2)} damage `)
  30.         });
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement