tedo1111

Untitled

Feb 14th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. function movingTarget(input) {
  2. let list = input.shift().slice().split(" ");
  3. let curEl = input.shift();
  4.  
  5. while (curEl !== `End`) {
  6. let elements = curEl.split(" ");
  7. let command = elements.shift();
  8. let firstIndex = Number(elements.shift());
  9. let secondIndex = Number(elements.shift());
  10.  
  11. if (command === `Shoot`) {
  12. if (firstIndex < list.length) {
  13. let res = Number(list[firstIndex]) - Number(secondIndex);
  14. if (res <= 0) {
  15. list.splice(firstIndex, 1);
  16. } else {
  17. list[firstIndex] = res;
  18. }
  19. }
  20. } else if (command === `Add`) {
  21. if (firstIndex < list.length) {
  22. list.splice(firstIndex, 0, secondIndex);
  23. } else {
  24. console.log("Invalid placement!");
  25. }
  26. } else if (command === `Strike`) {
  27. let start = firstIndex - secondIndex;
  28. let end = firstIndex + secondIndex;
  29. if (start >= 0 && end < list.length) {
  30. list.splice(start, end - start + 1);
  31. } else {
  32. console.log(`Strike missed!`);
  33. }
  34. }
  35.  
  36. curEl = input.shift();
  37. }
  38.  
  39. console.log(list.join(`|`));
  40. }
  41.  
  42. movingTarget((["1 2 3 4 5", "Strike 0 1", "End"]));
  43. movingTarget((["52 74 23 44 96 110",
  44. "Shoot 5 10",
  45. "Shoot 1 80",
  46. "Strike 2 1",
  47. "Add 22 3",
  48. "End"])
  49. );
  50.  
Advertisement
Add Comment
Please, Sign In to add comment