Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let pattern = /[^0-9\+\-\*\/\.]/;
- let pattern2 = /(-?\d+\.?\d*)|([\*\/])/g;
- let workNames = input.split(/[\,][\s*]/);
- let arr = [];
- workNames = workNames.map(el => el);
- for (let el of workNames) {
- let filteredName = el.split(/[0-9\+\-\*\/\.]/);
- let arithSymbols = el.split(/[^-?\d+\.?\d*]|\*+/);
- let multDiv = el.split(/[^\*\/]+/g);
- multDiv = multDiv.filter(el2 => el2 !== '');
- multDiv = multDiv.map(el2 => el2.split(''));
- multDiv = multDiv.flat();
- arithSymbols = arithSymbols.filter(el2 => el2 !== '');
- filteredName = filteredName.filter(el2 => el2 !== '');
- filteredName = filteredName.join('').split('');
- // console.log(filteredName);
- let sum = arithSymbols.reduce((acc, el3) => acc + Number(el3), 0);
- sum = multDiv.reduce((acc, el3) => el3.match(/[*]/) ? acc * 2 : acc / 2, sum);
- let totalHealth = filteredName.reduce((acc, el2) => acc + el2.charCodeAt(), 0);
- arr.push([el, { health: totalHealth, damage: sum.toFixed(2) }]);
- }
- arr.sort((a, b) => a[0].localeCompare(b[0]));
- // console.log(arr);
- for (let el of arr) {
- console.log(`${el[0]} - ${el[1]['health']} health, ${el[1]['damage']} damage`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment