Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. function solve(input = []){
  2.  
  3. let tanks = input.shift().split(`, `);
  4. let commandsCount = input.shift();
  5.  
  6. for(let i = 0; i < commandsCount; i++){
  7.  
  8. let tokens = input.shift().split(`, `);
  9. let action = tokens[0];
  10. let nameOrIndex = tokens[1];
  11. let secondName = tokens[2];
  12.  
  13. if(action === `Add`){
  14. if(tanks.includes(nameOrIndex)){
  15. console.log(`Tank is already bought`);
  16. }
  17. else{
  18. tanks.push(nameOrIndex);
  19. console.log(`Tank successfully bought`);
  20. }
  21. }
  22. else if(action === `Insert`){
  23. let index = Number(nameOrIndex);
  24. if(!tanks[index]){
  25. console.log(`Index out of range`);
  26. }
  27. else{
  28. if(!tanks.includes(secondName)){
  29. tanks.splice(index, 0, secondName)
  30. console.log(`Tank successfully bought`);
  31. }
  32. else{
  33. console.log(`Tank is already bought`);
  34. }
  35. }
  36. }
  37. else if(action === `Remove`){
  38. if(tanks.includes(nameOrIndex)){
  39. tanks.splice(tanks.indexOf(nameOrIndex), 1)
  40. console.log(`Tank successfully sold`);
  41. }
  42. else{
  43. console.log(`Tank not found`);
  44. }
  45. }
  46. else if(action === `Remove At`){
  47. let index = Number(nameOrIndex);
  48. if(!tanks[index]){
  49. console.log(`Index out of range`);
  50. }
  51. else{
  52. tanks.splice(index, 1)
  53. console.log(`Tank successfully sold`);
  54. }
  55. }
  56. }
  57. console.log(tanks.join(`, `))
  58. }
  59.  
  60. /*solve([`T-34-85 Rudy, SU-100Y, STG`,
  61. `3`,
  62. `Add, King Tiger(C)`,
  63. `Insert, 2, IS-2M`,
  64. `Remove, T-34-85 Rudy`,
  65. ])*/
  66.  
  67. solve([`T 34, T 34 B, T92, AMX 13 57`,
  68. `4`,
  69. `Add, T 34`,
  70. `Remove, AMX CDC`,
  71. `Insert, 10, M60`,
  72. `Remove At, 1`
  73. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement