Advertisement
Pijomir

Inventory

Oct 20th, 2023
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manageAnInventory(input) {
  2.     let journal = input.shift().split(', ');
  3.     let commands = { 'Collect': collect, 'Drop': drop, 'Combine Items': combineItems, 'Renew': renew };
  4.     for (let el of input) {
  5.         if (el === 'Craft!') {
  6.             break;
  7.         }
  8.  
  9.         let [command, items] = el.split(' - ');
  10.         if (command === 'Combine Items') {
  11.             let [item1, item2] = items.split(':');
  12.             journal = commands[command](journal, item1, item2);
  13.         } else {
  14.             journal = commands[command](journal, items);
  15.         }
  16.     }
  17.  
  18.     console.log(journal.join(', '));
  19.  
  20.     function collect(arr, item) {
  21.         if (!arr.includes(item)) {
  22.             arr.push(item);
  23.         }
  24.  
  25.         return arr;
  26.     }
  27.  
  28.     function drop(arr, item) {
  29.         if (arr.includes(item)) {
  30.             arr = arr.filter(a => a !== item);
  31.         }
  32.  
  33.         return arr;
  34.     }
  35.  
  36.     function combineItems(arr, oldItem, newItem) {
  37.         if (arr.includes(oldItem)) {
  38.             let startIndex = arr.indexOf(oldItem);
  39.             arr.splice(startIndex + 1, 0, newItem);
  40.         }
  41.  
  42.         return arr
  43.     }
  44.  
  45.     function renew(arr, item) {
  46.         if (arr.includes(item)) {
  47.             let startIndex = arr.indexOf(item);
  48.             let theRenewedItem = arr.splice(startIndex, 1);
  49.             arr.push(theRenewedItem);
  50.         }
  51.  
  52.         return arr;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement