simeonshopov

Inventory

Apr 1st, 2021 (edited)
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.   let heroes = [];
  3.  
  4.   class Hero {
  5.     constructor(name, level, items) {
  6.       this.name = name;
  7.       this.level = level;
  8.       this.items = items;
  9.     }
  10.   }
  11.  
  12.   for (let info of arr) {
  13.     info = info.split(' / ');
  14.     const name = info[0];
  15.     const level = Number(info[1]);
  16.     const items = info[2].split(', ').sort((a, b) => a.localeCompare(b));
  17.     heroes.push(new Hero(name, level, items));
  18.   }
  19.  
  20.   for (const hero of heroes.sort((a, b) => (a.level - b.level))) {
  21.     console.log(`Hero: ${hero.name}\nlevel => ${hero.level}`);
  22.     if (hero.items) { console.log(`items => ${hero.items.join(', ')}`)}
  23.   }
  24. }
Add Comment
Please, Sign In to add comment