Advertisement
dimoBs

inventory

Feb 21st, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(array) {
  2.  
  3.     let copy = array.slice();
  4.     let inventory = copy[0].split(', ')
  5.    
  6.     for (let i = 1; i < array.length; i++) {
  7.         if (array[i].includes(' - ')) {
  8.             let input = array[i].split(' - ');
  9.             let [command, ...intem] = input;
  10.             let current = intem[0];
  11.             if (command === 'Collect') {
  12.                 if (!inventory.includes(current)) {
  13.                     inventory.push(current)
  14.                 }
  15.  
  16.             }else if (command === 'Drop'){
  17.                 if (inventory.includes(current)){
  18.                     inventory.splice(inventory.indexOf(current), 1)
  19.                 }
  20.                
  21.             }else if(command === 'Combine Items'){
  22.                 let [oldItem, newItem] = current.split(':');
  23.                 if (inventory.includes(oldItem)){
  24.                     let indexToPlace = inventory.indexOf(oldItem);
  25.                     inventory.splice(indexToPlace+1, 0, newItem)
  26.                 }  
  27.  
  28.             }else if(command === 'Renew'){
  29.                 if (inventory.includes(current)){
  30.                     let indexToPlace = inventory.indexOf(current);
  31.                     inventory.splice(inventory.indexOf(current), 1)
  32.                     inventory.push(current);
  33.                 }
  34.             }
  35.         } else if (array[i] === 'Craft!') {
  36.             console.log(inventory.join(', '));
  37.         }
  38.     }
  39. }
  40. solve(['Iron, Wood, Sword', 'Collect - Gold', 'Drop - Wood', 'Craft!'])
  41. solve(['Iron, Sword', 'Drop - Bronze', 'Combine Items - Sword:Bow', 'Renew - Iron', 'Craft!'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement