Advertisement
krasizorbov

Moving Targets

Oct 30th, 2020
2,327
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.     let command = input.shift();
  4.  
  5.     while(command !== 'End'){
  6.         let curCommand = command.split(' ')[0];
  7.         let firstElement = command.split(' ')[1];
  8.         let secondElement = command.split(' ')[2];
  9.  
  10.         switch(curCommand){
  11.             case 'Shoot' :
  12.                 targets = shootCase(targets, firstElement, secondElement)
  13.                 break;
  14.             case 'Add' :
  15.                 targets = addCase(targets, firstElement, secondElement);
  16.                 break;
  17.             case 'Strike' :
  18.                 targets = strikeCase(targets, firstElement, secondElement);
  19.                 break;
  20.         }
  21.         command = input.shift();
  22.     }
  23.  
  24.     console.log(targets.join("|"));
  25.  
  26.     function shootCase(arr, index, power){
  27.         index = Number(index);
  28.         power = Number(power);
  29.  
  30.         if(index < 0 || index >= arr.length){
  31.             return arr;
  32.         }
  33.  
  34.         arr[index] -= power;
  35.  
  36.         if(arr[index] <= 0){
  37.             arr = arr.filter((x, y) => y != index)
  38.         }
  39.  
  40.         return arr;
  41.     }
  42.  
  43.     function addCase(arr, index, value){
  44.         index = Number(index);
  45.         value = Number(value);
  46.         if(index < 0 || index >= arr.length){
  47.             console.log('Invalid placement!');
  48.             return arr;
  49.         }
  50.  
  51.         arr.splice(index, 0, value);
  52.         return arr;
  53.     }
  54.  
  55.     function strikeCase(arr, index, radius){
  56.         index = Number(index);
  57.         radius = Number(radius);
  58.  
  59.         let lelftIndex = index - radius;
  60.         let rightIndex = index + radius;
  61.  
  62.         if(index >= 0 && index < arr.length){
  63.             if (lelftIndex >= 0 && rightIndex < arr.length) {
  64.                 arr.splice(lelftIndex, radius + radius + 1);
  65.             }else{
  66.                 console.log('Strike missed!');
  67.                 return arr;
  68.             }
  69.            
  70.         }
  71.         return arr;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement