divanov94

Untitled

Jul 2nd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function inventory(input) {
  2.     let inventory = input.shift().split(", ");
  3.    
  4.  
  5.     for (const commands of input) {
  6.         let command = input.shift();
  7.         if (command !== "Craft!") {
  8.             let splitCommand = command.split(" - ");
  9.             let action = splitCommand[0];
  10.             let item = splitCommand[1];
  11.             switch (action) {
  12.                 case "Collect":
  13.                     if (!inventory.includes(item)) {
  14.                         inventory.push(item);
  15.  
  16.  
  17.  
  18.                     }
  19.                     break;
  20.                 case "Drop":
  21.                     let itemIndex = inventory.indexOf(item);
  22.                     if (itemIndex != -1) {
  23.                         inventory.splice(itemIndex, 1)
  24.  
  25.  
  26.                     }
  27.                     break;
  28.                 case "Combine Items":
  29.                     let [oldItem, newItem] = item.split(":");
  30.                     let oldIndex = inventory.indexOf(oldItem);
  31.                     if (oldIndex != -1) {
  32.                         inventory.splice(oldIndex + 1, 0, newItem);
  33.                     }
  34.  
  35.  
  36.                     break;
  37.                 case "Renew":
  38.                     itemIndex = inventory.indexOf(item);
  39.                     if (itemIndex != -1) {
  40.                         inventory.splice(itemIndex, 1);
  41.                         inventory.push(item);
  42.                     }
  43.  
  44.  
  45.                     break;
  46.             }
  47.  
  48.  
  49.         }
  50.  
  51.  
  52.  
  53.  
  54.     }
  55.     console.log(inventory.join(","));
  56.  
  57.  
  58.  
  59. }
Add Comment
Please, Sign In to add comment