Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function inventory(input) {
- let index = 0;
- let itemList = input[index].split(`, `);
- index++;
- let commandLine = input[index];
- index++;
- while (commandLine !== "Craft!") {
- let commands = commandLine.split(` - `);
- let command = commands[0];
- let item;
- let item2;
- if (commands[1].includes(`:`)) {
- let combineList = commands[1].split(`:`);
- item = combineList[0];
- item2 = combineList[1];
- } else {
- item = commands[1];
- }
- if (!itemList.includes(item) && command === "Collect") {
- itemList.push(item);
- }
- if (itemList.includes(item)) {
- let itemIndex = itemList.indexOf(item);
- switch (command) {
- case "Drop":
- itemList.splice(itemIndex, 1);
- break;
- case "Combine Items":
- itemList.splice(++itemIndex, 0, item2);
- break;
- case "Renew":
- itemList.splice(itemIndex, 1);
- itemList.push(item);
- break;
- }
- }
- commandLine = input[index];
- index++;
- }
- console.log(itemList.join(`, `));
- }
- inventory([
- 'Iron, Wood, Sword',
- 'Collect - Gold',
- 'Drop - Wood',
- 'Craft!'
- ]);
- inventory([
- 'Iron, Sword',
- 'Drop - Bronze',
- 'Combine Items - Sword:Bow',
- 'Renew - Iron',
- 'Craft!'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement