ALSAVOV

Untitled

Jun 17th, 2023
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.97 KB | Source Code | 0 0
  1.  
  2. function movingTrgt(data) {
  3.     let targets = data.shift().split(" ");
  4.  
  5.     targets = targets.map(Number);
  6.  
  7.     let index = 0;
  8.  
  9.     let command = data[index];
  10.     index++;
  11.  
  12.     while (command !== "End") {
  13.         let tokens = command.split(" ");
  14.  
  15.         let currentIndex = +tokens[1];
  16.         let currentProperty = +tokens[2];
  17.  
  18.         switch (tokens[0]) {
  19.             case "Shoot":
  20.                 if (targets[currentIndex] !== undefined) {
  21.                     if (targets[currentIndex] - currentProperty <= 0) {
  22.                         targets.splice(currentIndex, 1);
  23.                     } else {
  24.                         targets.splice(
  25.                             currentIndex,
  26.                             1,
  27.                             targets[currentIndex] - currentProperty
  28.                         );
  29.                     }
  30.                 }
  31.                 break;
  32.             case "Add":
  33.                 if (targets[currentIndex] !== undefined) {
  34.                     targets.splice(currentIndex, 0, currentProperty);
  35.                 } else {
  36.                     console.log("Invalid placement!");
  37.                 }
  38.                 break;
  39.             case "Strike":
  40.                 if (targets[currentIndex] !== undefined) {
  41.                     if (
  42.                         targets[currentIndex - currentProperty] !== undefined &&
  43.                         targets[currentIndex + currentProperty] !== undefined
  44.                     ) {
  45.                         let elementsToRemove = 2 * currentProperty + 1;
  46.                         let startingIndexToRemove =
  47.                             currentIndex - currentProperty;
  48.                         targets.splice(startingIndexToRemove, elementsToRemove);
  49.                     } else {
  50.                         console.log("Strike missed!");
  51.                     }
  52.                 }
  53.                 break;
  54.         }
  55.  
  56.         command = data[index];
  57.         index++;
  58.     }
  59.     console.log(targets.join("|"));
  60. }
Advertisement
Add Comment
Please, Sign In to add comment