Guest User

Untitled

a guest
Jun 3rd, 2020
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. function movingTarget(arrInput) {
  2. let commands = arrInput.slice();
  3. let targets = commands.shift().split(' ').map(Number);
  4. let currentCommands = commands.shift();
  5.  
  6. let shoot = (indexInput, powerInput) => {
  7. let index = Number(indexInput);
  8. let power = Number(powerInput);
  9.  
  10. if (index < targets.length) {
  11. targets[index] -= power;
  12. }
  13.  
  14. if (targets[index] <= 0) {
  15. targets.splice(index, 1)
  16. }
  17.  
  18. return targets;
  19. }
  20.  
  21. let add = (indexInput, valueInput) => {
  22. let index = Number(indexInput);
  23. let value = Number(valueInput);
  24.  
  25. if (index < targets.length) {
  26. targets.splice(index, 0, value);
  27. } else {
  28. console.log('Invalid placement!');
  29. }
  30.  
  31. return targets;
  32. }
  33.  
  34. let strike = (indexInput, radiusInput) => {
  35. let index = Number(indexInput);
  36. let radius = Number(radiusInput);
  37. let startIndex = index - radius;
  38. let endIndex = index + radius;
  39. let targetsRemove = radius + radius + 1;
  40.  
  41. if (startIndex >= 0 && endIndex < targets.length) {
  42. targets.splice(startIndex, targetsRemove);
  43. } else {
  44. console.log('Strike missed!');
  45. }
  46.  
  47. return targets;
  48. }
  49.  
  50. while (currentCommands != 'End') {
  51. let [command, index, number] = currentCommands.split(' ');
  52.  
  53. if (command == 'Shoot') {
  54. targets = shoot(index, number);
  55. } else if (command == 'Add') {
  56. targets = add(index, number);
  57. } else if (command == 'Strike') {
  58. targets = strike(index, number);
  59. }
  60.  
  61. currentCommands = commands.shift();
  62. }
  63.  
  64. console.log(targets.join('|'));
  65. }
Add Comment
Please, Sign In to add comment