Advertisement
Spocoman

03. Inventory

Nov 4th, 2023
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function inventory(input) {
  2.     let collectedItems = input[0].split(", ");
  3.  
  4.     for (let i = 1; input[i] !== "Craft!"; i++) {
  5.         [command, item] = input[i].split(" - ");
  6.         if (command === "Collect") {
  7.             if (!collectedItems.includes(item)) {
  8.                 collectedItems.push(item);
  9.             }
  10.         } else if (command === "Drop") {
  11.             if (collectedItems.includes(item)) {
  12.                 collectedItems.splice(collectedItems.indexOf(item), 1);
  13.             }
  14.         } else if (command === "Combine Items") {
  15.             [oldItem, newItem] = item.split(':');
  16.             if (collectedItems.includes(oldItem)) {
  17.                 collectedItems.splice(collectedItems.indexOf(oldItem) + 1, 0, newItem);
  18.             }
  19.         } else {
  20.             if (collectedItems.includes(item)) {
  21.                 collectedItems.splice(collectedItems.indexOf(item), 1);
  22.                 collectedItems.push(item);
  23.             }
  24.         }
  25.     }
  26.  
  27.     console.log(collectedItems.join(", "));
  28.     return;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement