Lulunga

Gladiator Inventory

Jun 20th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. function solve(input) {
  2. let inventory = input.shift().split(' ');
  3. let commands = input;
  4.  
  5. for (let i=0; i<commands.length;i++) {
  6. let tokens = commands[i].split(' ');
  7. if (tokens[0]==='Buy') {
  8. if (!inventory.includes(tokens[1])) {
  9. inventory.push(tokens[1]);
  10. }
  11.  
  12. } else if (tokens[0]==='Trash') {
  13. if (inventory.includes(tokens[1])) {
  14. inventory.splice(inventory.indexOf(tokens[1]), 1);
  15. }
  16. } else if (tokens[0]==='Repair') {
  17. if (inventory.includes(tokens[1])) {
  18. inventory.splice(inventory.indexOf(tokens[1]), 1);
  19. inventory.push(tokens[1]);
  20. }
  21. } else if (tokens[0]==='Upgrade') {
  22. let upgradeArray = tokens[1].split('-');
  23. let el=upgradeArray[0];
  24. if (inventory.includes(el)) {
  25.  
  26. inventory.splice(inventory.indexOf(el)+1, 0, el+':'+upgradeArray[1]);
  27. }
  28. }
  29. }
  30. console.log(inventory.join(' '));
  31. }
  32. /*solve(['SWORD Shield Spear',
  33. 'Buy Bag',
  34. 'Trash Shield',
  35. 'Repair Spear',
  36. 'Upgrade SWORD-Steel']
  37. );*/
Advertisement
Add Comment
Please, Sign In to add comment