Advertisement
Guest User

05. Nether Realms

a guest
Aug 3rd, 2022
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. function homework(input) {
  2. let demons = input.split(/,/g).map((el) => el.trim())
  3. let object = {}
  4. for (let demon of demons) {
  5. let health = 0
  6. let damage = 0
  7. if(demon.match(/[A-z]+/g) != null) {
  8. demon.match(/[A-z]+/g).join('').split('').forEach(char => {
  9. health += Number(char.charCodeAt())
  10. })
  11. }
  12. let getDamage = demon.match(/[+-]?\d+\.?\d*/g)
  13. if (getDamage != null) {
  14. getDamage.forEach(el => {
  15. damage += Number(el)
  16. })
  17. }
  18. let action = demon.match(/\*|\//g)
  19. if (action != null) {
  20. action.forEach(el => {
  21. if (el == '*') {
  22. damage *= 2;
  23. } else {
  24. damage /= 2;
  25. }
  26. })
  27. }
  28. if(!object.hasOwnProperty(demon) && demon.length != 0 ){
  29. object[demon] = {health:health, damage:damage}
  30. }
  31. }
  32. let sorted = Object.entries(object).sort((a, b) => {
  33. return a[0].localeCompare(b[0])
  34. })
  35. for(let [key, value] of sorted){
  36. console.log(`${key} - ${value.health} health, ${(value.damage).toFixed(2)} damage`);
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement