Todorov_Stanimir

8 zad

Jun 14th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function gladiatorInventory(input) {
  2.     let inventory = input.shift().split(' ');
  3.  
  4.     for (let i = 0; i < input.length; i++) {
  5.         let operation = input[i].split(' ');
  6.         let command = operation[0];
  7.         let newInventory = operation[1];
  8.  
  9.         if (command==='Buy') {
  10.             if (!inventory.includes(newInventory)){
  11.                 inventory.push(newInventory);
  12.             }
  13.         } else if (command==='Trash') {
  14.             if (inventory.includes(newInventory)){
  15.                 inventory.splice(inventory.indexOf(newInventory),1);
  16.             }
  17.         } else if (command==='Repair') {
  18.             if (inventory.includes(newInventory)){
  19.                 inventory.splice(inventory.indexOf(newInventory),1);
  20.                 inventory.push(newInventory);
  21.             }
  22.         } else if (command==='Upgrade') {
  23.             let inventoryForUpgrade=newInventory.split('-');
  24.             if (inventory.includes(inventoryForUpgrade[0])){
  25.                 inventory.splice(inventory.indexOf(inventoryForUpgrade[0])+1,0,inventoryForUpgrade[0]+':'+inventoryForUpgrade[1]);
  26.             }
  27.         }
  28.     }
  29.     console.log(inventory.join(' '));
  30. }
Advertisement
Add Comment
Please, Sign In to add comment