Advertisement
Spocoman

03. Moving Target

Nov 8th, 2023
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function movingTarget(input) {
  2.     let targets = input.shift().split(' ').map(Number);
  3.     [command, index, value] = input.shift().split(' ');
  4.  
  5.     while (command != "End") {
  6.         index = Number(index);
  7.         value = Number(value);
  8.         if (command === "Shoot") {
  9.             if (index >= 0 && index < targets.length) {
  10.                 targets[index] -= value;
  11.                 if (targets[index] <= 0) {
  12.                     targets.splice(index, 1);
  13.                 }
  14.             }
  15.         } else if (command === "Add") {
  16.             if (index >= 0 && index < targets.length) {
  17.                 targets.splice(index, 0, value);
  18.             } else {
  19.                 console.log("Invalid placement!");
  20.             }
  21.         } else if (command === "Strike") {
  22.             if (index - value >= 0 && index + value < targets.length) {
  23.                 targets.splice(index - value, value * 2 + 1);
  24.             } else {
  25.                 console.log("Strike missed!");
  26.             }
  27.         }
  28.         [command, index, value] = input.shift().split(' ');
  29.     }
  30.  
  31.     console.log(targets.join('|'));
  32.     return;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement