Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function myFunc(arr) {
- const result = [];
- for (let herosData of arr) {
- let [heroName, heroLevel, heroItems] = herosData.split(" / ");
- const newHeroObj = heroFactory(heroName, Number(heroLevel), heroItems);
- result.push(newHeroObj);
- }
- function heroFactory(name, level, items) {
- const heroObj = {
- name,
- level,
- items: items ? items.split(", ") : [],
- };
- return heroObj;
- }
- return JSON.stringify(result);
- }
- console.log(
- myFunc([
- "Isacc / 25 ",
- "Derek / 12 / BarrelVest, DestructionSword",
- "Hes / 1 / Desolator, Sentinel, Antara",
- ])
- );
Advertisement
Add Comment
Please, Sign In to add comment