viligen

herosRegistry

May 27th, 2022
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function myFunc(arr) {
  2.     const result = [];
  3.  
  4.     for (let herosData of arr) {
  5.         let [heroName, heroLevel, heroItems] = herosData.split(" / ");
  6.         const newHeroObj = heroFactory(heroName, Number(heroLevel), heroItems);
  7.         result.push(newHeroObj);
  8.     }
  9.  
  10.     function heroFactory(name, level, items) {
  11.         const heroObj = {
  12.             name,
  13.             level,
  14.             items: items ? items.split(", ") : [],
  15.         };
  16.         return heroObj;
  17.     }
  18.     return JSON.stringify(result);
  19. }
  20.  
  21. console.log(
  22.     myFunc([
  23.         "Isacc / 25 ",
  24.         "Derek / 12 / BarrelVest, DestructionSword",
  25.         "Hes / 1 / Desolator, Sentinel, Antara",
  26.     ])
  27. );
  28.  
Advertisement
Add Comment
Please, Sign In to add comment