Advertisement
Guest User

Untitled

a guest
Mar 6th, 2021
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  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. inventory.push(commands[1]);
  9. } else if (commands[0] === "Trash") {
  10. if (inventory.includes(commands[1])) {
  11. let index = inventory.indexOf(commands[1]);
  12. inventory.splice(index, 1);
  13. }
  14. } else if (commands[0] === "Repair") {
  15. if (inventory.includes(commands[1])) {
  16. let index = inventory.indexOf(commands[1]);
  17. inventory.splice(index, 1);
  18. inventory.push(commands[1]);
  19. }
  20. } else if (commands[0] === "Upgrade") {
  21. let upgadeElement = commands[1].split('-');
  22. if (inventory.includes(upgadeElement[0])) {
  23. let index = inventory.indexOf(upgadeElement[0]);
  24. inventory.splice(index + 1, 0, `${upgadeElement[0]}:${upgadeElement[1]}`);
  25. }
  26. }
  27. }
  28. console.log(inventory.join(' '));
  29. }
  30.  
  31. inventoryGladiator(['SWORD Shield Spear', 'Buy Bag', 'Trash Shield', 'Repair Spear', 'Upgrade SWORD-Steel']);
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement