Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function inventory(arr) {
- let items = arr.shift().split(', ');
- arr.pop();
- for (let i = 0; i < arr.length; i++) {
- let tokens = arr[i];
- let commands = tokens.split(' - ');
- let command = commands[0];
- let item = commands[1].split(':');
- let oldItem = item[0];
- let newItem = item[1];
- if (command === "Collect") {
- if (!(items.includes(oldItem))) {
- items.push(oldItem);
- }
- } else if (command === "Drop") {
- if ((items.includes(oldItem))) {
- let index = items.indexOf(oldItem);
- if (index !== -1) {
- items.splice(index, 1);
- }
- }
- } else if (command === "Combine Items") {
- if (items.includes(oldItem)) {
- let index = items.indexOf(oldItem);
- if (index !== -1) {
- items.splice(index + 1, 0, newItem)
- }
- }
- } else if (command === "Renew") {
- if (items.includes(oldItem)) {
- let index = items.indexOf(oldItem);
- if (index !== -1) {
- items.splice(index, 1);
- items.push(oldItem);
- }
- }
- }
- }
- console.log(items.join(', '));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement