dilyana2001

Untitled

Jul 23rd, 2021 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let heroes = {};
  3.     arr.forEach(line => {
  4.         let [heroName, skill, points] = line.split(' -> ');
  5.         points = Number(points);
  6.         if (heroName != 'Ave Cesar') {
  7.             if (points) {
  8.                 if (!heroes.hasOwnProperty(heroName)) {
  9.                     heroes[heroName] = [{ skill, points }];
  10.                 } else {
  11.                     for (let update in heroes[heroName]) {
  12.                         if (skill === heroes[heroName][update].skill) {
  13.                             if (points > heroes[heroName][update].points) {
  14.                                 heroes[heroName][update].points = points;
  15.                             }
  16.                         } else {
  17.                             heroes[heroName].push({ skill, points });
  18.                             break;
  19.                         }
  20.                     }
  21.                 }
  22.             } else {
  23.                 let [firstHero, secondHero] = heroName.split(' vs ')
  24.                 if (heroes.hasOwnProperty(firstHero) && heroes.hasOwnProperty(secondHero)) {
  25.                     Object.values(heroes[firstHero]).forEach(el1 => {
  26.                         Object.values(heroes[secondHero]).forEach(el2 => {
  27.                             if (el1.skill === el2.skill) {
  28.                                 let totalFirstHero = Object.values(heroes[firstHero]).reduce((a, b) => a + b.points, 0);
  29.                                 let totalSecondHero = Object.values(heroes[secondHero]).reduce((a, b) => a + b.points, 0);
  30.                                 if (totalFirstHero > totalSecondHero) {
  31.                                     delete heroes[secondHero];
  32.                                 } else {
  33.                                     delete heroes[firstHero];
  34.                                 }
  35.                             }
  36.                         });
  37.                     });
  38.                 }
  39.             }
  40.         }
  41.     });
  42.     Object.entries(heroes).sort((a, b) => b[1].reduce((a, b) => a + b.points, 0) - a[1].reduce((a, b) => a + b.points, 0))
  43.         .map(x => console.log(`${x[0]}: ${x[1].reduce((a, b) => a + b.points, 0)} skill\n${x[1]
  44.             .sort((a,b)=>a.skill.localeCompare(b.skill)).sort((a,b)=>b.points-a.points)
  45.             .map(x=>`- ${x.skill} <!> ${x.points}`).join('\n')}`));
  46. }
Advertisement
Add Comment
Please, Sign In to add comment