SKGreyHound

09. Gladiator Inventory

Jun 29th, 2020
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. function gladiatorInventory(inputCommands) {
  2.  
  3. let inventory = inputCommands.shift().split(' ');
  4.  
  5. for (let i = 0; i < inputCommands.length; i++) {
  6. let commands = inputCommands.slice()[i].split(' ')
  7. let command = commands[0];
  8. let equipment = commands[1];
  9.  
  10. switch (command) {
  11. case 'Buy': buy(equipment);
  12. break;
  13. case 'Trash': thrash(equipment);
  14. break;
  15. case 'Repair': repair(equipment);
  16. break;
  17. case 'Upgrade': upgrade(equipment);
  18. break;
  19. }
  20. }
  21. console.log(inventory.join(' '));
  22.  
  23. function buy(eqp) {
  24. if (!inventory.includes(eqp)) {
  25. inventory.push(eqp);
  26. }
  27. return inventory;
  28. }
  29.  
  30. function thrash(eqp) {
  31. for (let i = 0; i < inventory.length; i++) {
  32. if (eqp === inventory[i]) {
  33. inventory.splice(i, 1);
  34. }
  35. }
  36. return inventory;
  37. }
  38.  
  39. function repair(eqp) {
  40. for (let i = 0; i < inventory.length; i++) {
  41. if (eqp === inventory[i]) {
  42. let repaired = inventory.splice(i, 1);
  43. inventory.push(repaired.toString());
  44. }
  45. }
  46. return inventory;
  47. }
  48.  
  49. function upgrade(eqp) {
  50. let item = eqp.split('-')
  51. for (let i = 0; i < inventory.length; i++) {
  52. if (item[0] === inventory[i]) {
  53. inventory.splice(i+1,0,eqp)
  54. }
  55. }
  56. return inventory;
  57. }
  58.  
  59. }
Add Comment
Please, Sign In to add comment