Advertisement
TZinovieva

Heroes of Code and Logic VII JS Fundamentals

Mar 24th, 2023
1,836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function heroesOfCode(input) {
  2.     let heroesNumber = input.shift();
  3.     let maxHP = 100;
  4.     let maxMP = 200;
  5.     let heroName = '';
  6.     let currentHP = 0;
  7.     let currentMP = 0;
  8.     let heroPoints = {};
  9.     for (let lines = 0; lines < heroesNumber; lines++) {
  10.         let heroData = input.shift().split(' ');
  11.         heroName = heroData[0];
  12.         currentHP = heroData[1];
  13.         currentMP = heroData[2];
  14.         heroPoints[heroName] = { HP: Number(currentHP), MP: Number(currentMP) };
  15.     }
  16.     for (let line of input) {
  17.         let tokens = line.split(' - ');
  18.         let command = tokens[0];
  19.         let hero = tokens[1];
  20.         let parameters = tokens.slice(2);
  21.         let points = Number(parameters[0]);
  22.  
  23.         if (command === "CastSpell") {
  24.             if (heroPoints.hasOwnProperty(hero)) {
  25.                 if (heroPoints[hero].MP >= points) {
  26.                     heroPoints[hero].MP -= points;
  27.                     console.log(`${hero} has successfully cast ${parameters[1]} and now has ${heroPoints[hero].MP} MP!`);
  28.                 } else {
  29.                     console.log(`${hero} does not have enough MP to cast ${parameters[1]}!`);
  30.                 }
  31.             }
  32.         } else if (command === "TakeDamage") {
  33.             if (heroPoints.hasOwnProperty(hero)) {
  34.                 heroPoints[hero].HP -= points;
  35.                 if (heroPoints[hero].HP > 0) {
  36.                     console.log(`${hero} was hit for ${points} HP by ${parameters[1]} and now has ${heroPoints[hero].HP} HP left!`);
  37.                 } else {
  38.                     console.log(`${hero} has been killed by ${parameters[1]}!`);
  39.                 }
  40.             }
  41.         } else if (command === "Recharge") {
  42.             if (heroPoints.hasOwnProperty(hero)) {
  43.                 let rechargePoints = Math.min(points, maxMP - heroPoints[hero].MP);
  44.                 heroPoints[hero].MP += rechargePoints;
  45.                 console.log(`${hero} recharged for ${rechargePoints} MP!`);
  46.             }
  47.         } else if (command === "Heal") {
  48.             if (heroPoints.hasOwnProperty(hero)) {
  49.                 let healPoints = Math.min(points, maxHP - heroPoints[hero].HP);
  50.                 heroPoints[hero].HP += healPoints;
  51.                 console.log(`${hero} healed for ${healPoints} HP!`);
  52.             }
  53.         } else if (command === "End") {
  54.             break;
  55.         }
  56.     }
  57.  
  58.     for (let hero in heroPoints) {
  59.         if (heroPoints[hero].HP > 0) {
  60.             console.log(`${hero}\n  HP: ${heroPoints[hero].HP}\n  MP: ${heroPoints[hero].MP}`);
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement