dddilian

Inventory

Jun 23rd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(inputArr) {
  2.     let inventory = inputArr.shift().split(", ");
  3.  
  4.     for (let i = 0; i < inputArr.length; i++) {
  5.  
  6.         let tokens = inputArr[i].split(" - ");
  7.  
  8.         if (tokens[0] == "Collect") {
  9.             if (inventory.includes(tokens[1])) {
  10.                 continue;
  11.             } else {
  12.                 inventory.push(tokens[1]);
  13.             }
  14.  
  15.         } else if (tokens[0] == "Drop") {
  16.             if (inventory.includes(tokens[1])) {
  17.                 let idx = inventory.indexOf(tokens[1]);
  18.                 inventory.splice(idx, 1);
  19.             } else {
  20.                 continue;
  21.             }
  22.         } else if (tokens[0] == "Renew") {
  23.             if (inventory.includes(tokens[1])) {
  24.                 let idx = inventory.indexOf(tokens[1]);
  25.                 let item = inventory.splice(idx, 1);
  26.                 inventory.push(item);
  27.             } else {
  28.                 continue;
  29.             }
  30.         } else if (tokens[0] == "Combine Items") {
  31.             let items = tokens[1].split(":");
  32.  
  33.             if (inventory.includes(items[0])) {
  34.                 let idx = inventory.indexOf(items[0]);
  35.                 inventory.splice(idx+1, 0, items[1]);
  36.             } else {
  37.                 continue;
  38.             }
  39.         }
  40.  
  41.     }
  42.     console.log(inventory.join(", "));
  43. }
  44.  
  45. solve(['Iron, Wood, Sword', 'Collect - Gold', 'Drop - Wood', 'Craft!']);
  46.  
  47.  
  48. solve(['Iron, Sword', 'Drop - Bronze', 'Combine Items - Sword:Bow', 'Renew - Iron', 'Craft!'])
Add Comment
Please, Sign In to add comment