vladovip

Nether Realms_RegExp

Jan 3rd, 2023 (edited)
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (input){
  2. let regex = /,\s*/g;
  3. let collectionArrD = input.split(regex);
  4. // console.log(collectionArrD);
  5. let battleBookObj = {};
  6. let healthPattern = /[^0-9+\-*/\.]/g;
  7. let patternNums = /-?\d+(?:.\d+)?/g;
  8.  
  9. for (let currentLine of collectionArrD) {
  10.       //  TO find Health of the DEM
  11.     let name = currentLine;
  12.     let matches = currentLine.match(healthPattern);
  13.     let healtDEM = 0;
  14.     if(matches!== null){
  15.         for ( let x of matches) {
  16.             healtDEM+= x.charCodeAt(0);
  17.         }
  18.     }
  19.    
  20.     // TO find damage of DEM
  21.     let matchesNums = currentLine.match(patternNums);
  22.     let tempSumNums = 0;
  23.     if(matchesNums!== null){
  24.         for (let x of matchesNums) {
  25.             tempSumNums+= Number(x);
  26.         }
  27.     }
  28.     if (tempSumNums !==0){
  29.         for (let x of currentLine.split("")) {
  30.             if ( x == "*"){
  31.                 tempSumNums= tempSumNums*2;
  32.             } else if (x == "/"){
  33.                 tempSumNums = tempSumNums/2;
  34.             }
  35.        }
  36.     } else{
  37.         tempSumNums=0;
  38.     }
  39.     let damageDEM = tempSumNums;
  40.     if(battleBookObj.hasOwnProperty(name)== false){
  41.         battleBookObj[name] = { healtDEM,  damageDEM};  
  42.     }
  43.    
  44. }
  45. // console.log(battleBookObj);
  46. let sortedByName= Object.keys(battleBookObj).sort((a,b)=> a.localeCompare(b));
  47. for (let nameObj of sortedByName) {
  48.     console.log(`${nameObj} - ${battleBookObj[nameObj].healtDEM} health, ${battleBookObj[nameObj].damageDEM.toFixed(2)} damage`);
  49. }
  50.  
  51. }
  52.  
  53. solve("M3ph-0.5s-0.5t0.0**");
  54. console.log(`###########`);
  55.  
  56. solve("M3ph1st0**, Azazel");
  57.  
  58. console.log(`###########`);
  59. solve("Gos/ho")
Add Comment
Please, Sign In to add comment