Advertisement
Grossos

03-inventory.js

Oct 16th, 2023
1,588
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(inputArr) {
  2.  
  3.     let journal = inputArr.shift();
  4.     let inventory = journal.split(', ');
  5.  
  6.     let index = 0;
  7.     let command = inputArr[index];
  8.  
  9.     while (command !== 'Craft!') {
  10.         let tokens = command.split(' - ');
  11.         let doThis = tokens[0];
  12.         let item = tokens[1];
  13.  
  14.         if (doThis == 'Collect') {
  15.             if (inventory.includes(item)) {
  16.                 break;
  17.             } else {
  18.                 inventory.push(item);
  19.             }
  20.         } else if (doThis == 'Drop') {
  21.             if (inventory.includes(item)) {
  22.                 let indexForDel = inventory.indexOf(item);
  23.                 inventory.splice(indexForDel, 1);
  24.             }
  25.         } else if (doThis == 'Combine Items') {
  26.             let splitted = tokens[1].split(':');
  27.             let oldItem = splitted[0];
  28.             let newItem = splitted[1];
  29.             if (inventory.includes(oldItem)) {
  30.                 let oldItemIdx = inventory.indexOf(oldItem);
  31.                 inventory.splice(oldItemIdx + 1, 0, newItem);
  32.             }
  33.         } else if (doThis == 'Renew') {
  34.             // if (inventory.includes(item)) {
  35.             //     inventory = inventory.filter(x => x !== item);
  36.             //     inventory.push(item);
  37.             //  }
  38.             if (inventory.includes(item)) {
  39.                 let renewIdx = inventory.indexOf(item);
  40.                 let renewItem = inventory.splice(renewIdx, 1);
  41.                 inventory.push(renewItem.join(''));
  42.             }
  43.  
  44.         }
  45.         index++;
  46.         command = inputArr[index];
  47.  
  48.  
  49.     }
  50.  
  51.     console.log(inventory.join(', '));
  52.  
  53.  
  54. }
  55.  
  56.  
  57. // solve([
  58. //     'Iron, Wood, Sword',
  59. //     'Collect - Gold',
  60. //     'Drop - Wood',
  61. //     'Craft!'
  62. // ])
  63.  
  64. solve([
  65.     'Iron, Sword',
  66.     'Drop - Bronze',
  67.     'Combine Items - Sword:Bow',
  68.     'Renew - Iron',
  69.     'Craft!'
  70. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement