Liliana797979

gladiator inventory - fundamentals

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