Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function heroesOfCode(input) {
- let heroesNumber = input.shift();
- let maxHP = 100;
- let maxMP = 200;
- let heroName = '';
- let currentHP = 0;
- let currentMP = 0;
- let heroPoints = {};
- for (let lines = 0; lines < heroesNumber; lines++) {
- let heroData = input.shift().split(' ');
- heroName = heroData[0];
- currentHP = heroData[1];
- currentMP = heroData[2];
- heroPoints[heroName] = { HP: Number(currentHP), MP: Number(currentMP) };
- }
- for (let line of input) {
- let tokens = line.split(' - ');
- let command = tokens[0];
- let hero = tokens[1];
- let parameters = tokens.slice(2);
- let points = Number(parameters[0]);
- if (command === "CastSpell") {
- if (heroPoints.hasOwnProperty(hero)) {
- if (heroPoints[hero].MP >= points) {
- heroPoints[hero].MP -= points;
- console.log(`${hero} has successfully cast ${parameters[1]} and now has ${heroPoints[hero].MP} MP!`);
- } else {
- console.log(`${hero} does not have enough MP to cast ${parameters[1]}!`);
- }
- }
- } else if (command === "TakeDamage") {
- if (heroPoints.hasOwnProperty(hero)) {
- heroPoints[hero].HP -= points;
- if (heroPoints[hero].HP > 0) {
- console.log(`${hero} was hit for ${points} HP by ${parameters[1]} and now has ${heroPoints[hero].HP} HP left!`);
- } else {
- console.log(`${hero} has been killed by ${parameters[1]}!`);
- }
- }
- } else if (command === "Recharge") {
- if (heroPoints.hasOwnProperty(hero)) {
- let rechargePoints = Math.min(points, maxMP - heroPoints[hero].MP);
- heroPoints[hero].MP += rechargePoints;
- console.log(`${hero} recharged for ${rechargePoints} MP!`);
- }
- } else if (command === "Heal") {
- if (heroPoints.hasOwnProperty(hero)) {
- let healPoints = Math.min(points, maxHP - heroPoints[hero].HP);
- heroPoints[hero].HP += healPoints;
- console.log(`${hero} healed for ${healPoints} HP!`);
- }
- } else if (command === "End") {
- break;
- }
- }
- for (let hero in heroPoints) {
- if (heroPoints[hero].HP > 0) {
- console.log(`${hero}\n HP: ${heroPoints[hero].HP}\n MP: ${heroPoints[hero].MP}`);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement