Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let participants = {};
- let demonNames = arr.join().split(/[,][\s]*/).filter(e => e).map(e => e.trim()).filter(el => !el.includes(','));
- demonNames.forEach(line => {
- participants[line] = {};
- let health = line.match(/[^0123456789\+\-\.\*\/]/gm).map(e => e.charCodeAt(0)).reduce((a, b) => a + b, 0);
- let damage = line.match(/[+-]?[0-9]+[.]?[0-9]*/gm);
- if (!damage) {
- damage = 0.00;
- } else {
- damage = damage.map(Number).reduce((a, b) => a + b, 0);
- }
- let multiplyDivide = line.match(/[*\/]/gm);
- if (multiplyDivide) {
- multiplyDivide.forEach((symbol) => {
- switch (symbol) {
- case '*':
- damage *= 2;
- break;
- case '/':
- damage /= 2;
- break;
- }
- });
- }
- participants[line]['health'] = health;
- participants[line]['damage'] = damage.toFixed(2);
- });
- let result = Object.keys(participants)
- .sort()
- .forEach(key => {
- console.log(`${key} - ${participants[key].health} health, ${participants[key].damage} damage`);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement