Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function inventory(input) {
- let inventory = input.shift().split(", ");
- for (const commands of input) {
- let command = input.shift();
- if (command !== "Craft!") {
- let splitCommand = command.split(" - ");
- let action = splitCommand[0];
- let item = splitCommand[1];
- switch (action) {
- case "Collect":
- if (!inventory.includes(item)) {
- inventory.push(item);
- }
- break;
- case "Drop":
- let itemIndex = inventory.indexOf(item);
- if (itemIndex != -1) {
- inventory.splice(itemIndex, 1)
- }
- break;
- case "Combine Items":
- let [oldItem, newItem] = item.split(":");
- let oldIndex = inventory.indexOf(oldItem);
- if (oldIndex != -1) {
- inventory.splice(oldIndex + 1, 0, newItem);
- }
- break;
- case "Renew":
- itemIndex = inventory.indexOf(item);
- if (itemIndex != -1) {
- inventory.splice(itemIndex, 1);
- inventory.push(item);
- }
- break;
- }
- }
- }
- console.log(inventory.join(","));
- }
Add Comment
Please, Sign In to add comment