Advertisement
pacho_the_python

Untitled

Mar 10th, 2023
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function inventory(data) {
  2.     class Hero {
  3.         constructor(name, level, items) {
  4.             this.name = name
  5.             this.level = level
  6.             this.items = items
  7.         }
  8.         getHeroInfo() {
  9.             console.log(`Hero: ${this.name}\nlevel => ${this.level}\nitems => ${this.items}`)
  10.         }
  11.     }
  12.     let heroes = []
  13.     while (data.length > 0) {
  14.         let heroData = data.shift().split(' / ')
  15.         let heroName = heroData[0]
  16.         let heroLevel = Number(heroData[1])
  17.         let heroInventory = heroData[2]
  18.         let newHero = new Hero(heroName, heroLevel, heroInventory)
  19.         heroes.push(newHero)
  20.     }
  21.     heroes.sort((a, b) => a.level - b.level)
  22.     for (const hero of heroes) {
  23.         hero.getHeroInfo()
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement