Advertisement
Guest User

Inventory

a guest
Oct 30th, 2019
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function inventory(arr){
  2.     const heroes = [];
  3.    
  4.     for(let element of arr){
  5.         element = element.split(' / ');
  6.        
  7.         let heroName = element.shift();
  8.         let level = element.shift();
  9.         let items = element.join('').split(', ').sort();
  10.        
  11.         const hero = {
  12.             name : heroName,
  13.             level : Number(level),
  14.             items : items
  15.         }
  16.        
  17.         heroes.push(hero);
  18.     }
  19.    
  20.     const heroesAscending = [];
  21.     heroesAscending.push(heroes.shift());
  22.    
  23.  
  24.     for(let hero of heroes){
  25.         let level = hero.level;
  26.        
  27.         if(level < heroesAscending[0].level){
  28.             heroesAscending.unshift(hero);
  29.         }else if(level > heroesAscending[heroesAscending.length - 1].level){
  30.             heroesAscending.push(hero);
  31.         }
  32.     }
  33.    
  34.     for(let hero of heroesAscending){
  35.         console.log(`Hero: ${hero.name}\nlevel => ${hero.level}\nitems => ${hero.items.join(', ')}`);
  36.     }
  37.    
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement