Advertisement
kstoyanov

05. Inventory js fundamentals

Jul 2nd, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   const arrHeroes = [];
  3.  
  4.   args.forEach((element) => {
  5.     const hero = {};
  6.     const heroInfo = element.split('/');
  7.     const [heroName, heroLevel, heroItems] = heroInfo;
  8.  
  9.     hero.name = heroName.trimEnd();
  10.     hero.level = Number(heroLevel);
  11.     hero.items = heroItems.split(',');
  12.  
  13.     arrHeroes.push(hero);
  14.   });
  15.  
  16.   arrHeroes.sort((a, b) => a.level - b.level);
  17.  
  18.   Object.keys(arrHeroes).forEach((element) => {
  19.     const { items } = arrHeroes[element];
  20.     items.sort((a, b) => a.trimStart().localeCompare(b.trimStart()));
  21.  
  22.     console.log(`Hero: ${arrHeroes[element].name}`);
  23.     console.log(`level => ${arrHeroes[element].level}`);
  24.     console.log(`items =>${items}`);
  25.   });
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement