Guest User

netherRealms

a guest
Jul 11th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. function netherRealms(input) {
  2. let array = input.split(/,\s*/);
  3. let healthPattern = /([^0-9+\-*\/\.+])/g ;
  4. let demagePatern = /[-+]?\d+(\.\d+)?/g;
  5. let divideOrMultiply = /(?<multiply>[*])|(?<divide>[\/])/g;
  6. let namePattern = /[\s[^\,]/g
  7. let resultHealth = 0
  8. let resultDemage = 0
  9. let object = {};
  10. for (let string of array) {
  11. let isTrue = namePattern.test(string)
  12. if (string.length > 0 && isTrue === false){
  13. object[string]={}
  14. let health = [...string.matchAll(healthPattern)]
  15. let demage = [...string.matchAll(demagePatern)]
  16. let divideOrMultiplies = [...string.matchAll(divideOrMultiply)]
  17. for (let i = 0; i < health.length; i++) {
  18. let text = health[i][0]
  19. let textAsCodeAscii = text.charCodeAt()
  20. resultHealth +=textAsCodeAscii
  21. }
  22. object[string]['health'] = resultHealth
  23. resultHealth = 0;
  24. for (let x = 0; x < demage.length; x++) {
  25. let number = Number(demage[x][0])
  26. resultDemage +=number
  27. }
  28. for (let el of divideOrMultiplies) {
  29. if (el.groups.multiply === '*'){
  30. resultDemage*=2
  31. }else if (el.groups.divide === '/'){
  32. resultDemage/=2
  33. }
  34. }
  35. object[string]['damage'] = resultDemage;
  36. resultDemage = 0;
  37. }
  38. }
  39. let arrNewMap = Object.entries(object).sort((a,b)=> (a[0]).localeCompare(b[0]))
  40. for (let [name, arrays] of arrNewMap) {
  41. console.log(`${name} - ${arrays['health']} health, ${arrays['damage'].toFixed(2)} damage`)
  42. }
  43. }
  44. netherRealms('bla,Aza44454zel//,Azazel');
Add Comment
Please, Sign In to add comment