Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let records = {};
- function add(person, health, energy) {
- if (!records.hasOwnProperty(person)) {
- records[person] = {};
- records[person].health = 0;
- records[person].energy = energy;
- }
- records[person].health += health;
- };
- function attack(attackerName, defenderName, damage) {
- if (records[attackerName] && records[defenderName]) {
- records[defenderName].health -= damage;
- }
- if (records[defenderName].health <= 0) {
- console.log(`${defenderName} was disqualified!`);
- delete records[defenderName];
- }
- records[attackerName].energy -= 1;
- if (records[attackerName].energy <= 0) { //may be without <
- console.log(`${attackerName} was disqualified!`);
- delete records[attackerName];
- }
- };
- function deletion(userName) {
- if (userName === "All") {
- for (let prop in records) {
- delete records[prop];
- }
- } else {
- if (records.hasOwnProperty(userName)) {
- delete records[userName];
- }
- }
- };
- for (let line of input) {
- if (line === 'Results') {
- break;
- }
- let tokens = line.split(':');
- if (tokens !== undefined && tokens !== null) {
- let command = tokens.shift();
- if (command === 'Add') {
- let person = tokens[0];
- let health = Number(tokens[1]);
- let energy = Number(tokens[2]);
- add(person, health, energy);
- } else if (command === 'Attack') {
- let attackerName = tokens[0];
- let defenderName = tokens[1];
- let damage = Number(tokens[2]);
- attack(attackerName, defenderName, damage);
- } else if (command === 'Delete') {
- let username = tokens[0];
- deletion(username);
- }
- }
- }
- let countofPeople = 0;
- countofPeople = Object.keys(records).length;
- if (countofPeople !== 0) {
- console.log(`People count: ${countofPeople}`);
- }
- let sorted = Object.entries(records);
- sorted.sort((a, b) => b[1].health - a[1].health || a[0].localeCompare(b[0]));
- if (sorted.length > 0) {
- sorted.forEach(element => {
- console.log(`${element[0]} - ${element[1].health} - ${element[1].energy}`);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement