Advertisement
-Enigmos-

inventory.js

Feb 19th, 2022
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function inventory(input) {
  2.     let index = 0;
  3.     let itemList = input[index].split(`, `);
  4.     index++;
  5.     let commandLine = input[index];
  6.     index++;
  7.  
  8.     while (commandLine !== "Craft!") {
  9.         let commands = commandLine.split(` - `);
  10.         let command = commands[0];
  11.         let item;
  12.         let item2;
  13.         if (commands[1].includes(`:`)) {
  14.             let combineList = commands[1].split(`:`);
  15.             item = combineList[0];
  16.             item2 = combineList[1];
  17.         } else {
  18.             item = commands[1];
  19.         }
  20.        
  21.         if (!itemList.includes(item) && command === "Collect") {
  22.             itemList.push(item);
  23.         }
  24.  
  25.         if (itemList.includes(item)) {
  26.             let itemIndex = itemList.indexOf(item);
  27.             switch (command) {
  28.                 case "Drop":
  29.                     itemList.splice(itemIndex, 1);
  30.                     break;
  31.                 case "Combine Items":
  32.                     itemList.splice(++itemIndex, 0, item2);
  33.                     break;
  34.                 case "Renew":
  35.                     itemList.splice(itemIndex, 1);
  36.                     itemList.push(item);
  37.                     break;
  38.             }
  39.         }
  40.         commandLine = input[index];
  41.         index++;
  42.     }
  43.     console.log(itemList.join(`, `));
  44. }
  45.  
  46. inventory([
  47.     'Iron, Wood, Sword',
  48.     'Collect - Gold',
  49.     'Drop - Wood',
  50.     'Craft!'
  51. ]);
  52. inventory([
  53.     'Iron, Sword',
  54.     'Drop - Bronze',
  55.     'Combine Items - Sword:Bow',
  56.     'Renew - Iron',
  57.     'Craft!'
  58. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement