Guest User

LegendaryFarming

a guest
Jul 18th, 2020
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(params) {
  2.     let str = params.split(' ');
  3.     const materials = {};
  4.     const legendaryItems = {
  5.         shards: 'Shadowmourne',
  6.         fragments: 'Valanyr',
  7.         motes: 'Dragonwrath',
  8.     };
  9.     let result = '';
  10.     for (let i = 0; i < str.length; i += 2) {
  11.         let quantity = Number(str[i]);
  12.         let material = str[i + 1].toLowerCase();
  13.         if (!materials.hasOwnProperty(material)) {
  14.             materials[material] = 0;
  15.         }
  16.         materials[material] += quantity;
  17.         if (materials[material] >= 250 && material in legendaryItems) {
  18.             result += `${legendaryItems[material]} obtained!\n`;
  19.             materials[material] -= 250;
  20.             break;
  21.         }
  22.     }
  23.     for (let item in legendaryItems) {
  24.         if (!materials.hasOwnProperty(item)) {
  25.             materials[item] = 0;
  26.         }
  27.     }
  28.     const legendaryItemsCollected = Object.entries(materials)
  29.         .filter((item) => hasLegendaryItems(legendaryItems, item[0]))
  30.         .sort((a, b) => sortDescending(a[1], b[1]) || sortAlphabetically(a[0], b[0]))
  31.         .map(formatText)
  32.         .join('\n');
  33.     const junkItemsCollected = Object.entries(materials)
  34.         .filter((item) => !hasLegendaryItems(legendaryItems, item[0]))
  35.         .sort((a, b) => sortAlphabetically(a[0], b[0]))
  36.         .map(formatText)
  37.         .join('\n');
  38.     result += legendaryItemsCollected + '\n' + junkItemsCollected;
  39.  
  40.     function hasLegendaryItems(items, item) {
  41.         return items.hasOwnProperty(item);
  42.     }
  43.  
  44.     function formatText(textArr) {
  45.         return `${textArr[0]}: ${textArr[1]}`;
  46.     }
  47.  
  48.     function sortAlphabetically(a, b) {
  49.         return a.localeCompare(b);
  50.     }
  51.  
  52.     function sortDescending(a, b) {
  53.         return b - a;
  54.     }
  55.  
  56.     return result;
  57. }
Add Comment
Please, Sign In to add comment