Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let pattern = /[0-9\+\-\*\/\.]/;
- let pattern2 = /[^\-|\+?\d+\.?\d+]|[\*\/]|(?<=\D)\.|\.(?=\D)/g;
- let workNames = input.split(/[\,][\s]*/);
- let arr = [];
- workNames = workNames.map(el => el);
- workNames.sort((a, b) => a.localeCompare(b));
- // console.log(workNames);
- for (let el of workNames) {
- let filteredName = el.split(pattern);
- let arithSymbols = el.split(pattern2);
- 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);
- let sum2 = 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: sum2.toFixed(2) }]);
- }
- // 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
Advertisement