Advertisement
dilyana2001

Untitled

Jul 20th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(str) {
  2.     let items = { shards: 0, motes: 0, fragments: 0 };
  3.     let junks = {};
  4.     str = str.split(' ');
  5.     for (let i = 0; i < str.length; i += 2) {
  6.         let quantity = Number(str[i]);
  7.         let material = str[i + 1].toLowerCase();
  8.         if (items.hasOwnProperty(material)) {
  9.             if (items[material] + quantity < 250) items[material] += quantity;
  10.             else {
  11.                 if (material === 'shards') console.log(`Shadowmourne obtained!`);
  12.                 else if (material === 'fragments') console.log(`Valanyr obtained!`);
  13.                 else if (material === 'motes') console.log(`Dragonwrath obtained!`)
  14.                 items[material] += quantity;
  15.                 items[material] -= 250;
  16.                 break;
  17.             }
  18.         } else {
  19.             !junks.hasOwnProperty(material) ? junks[material] = quantity : junks[material] += quantity;
  20.         }
  21.     }
  22.     Object.entries(items).sort((a, b) => a[0].localeCompare(b[0])).sort((a, b) => b[1] - a[1]).map((x => console.log(`${x[0]}: ${x[1]}`)));
  23.     Object.entries(junks).sort((a, b) => a[0].localeCompare(b[0])).map((x => console.log(`${x[0]}: ${x[1]}`)));
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement