Advertisement
Guest User

movingTarget

a guest
Apr 23rd, 2020
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function movingTarget(input = [] ) {
  2.  
  3.   let target = input.shift().split(` `).map(Number)
  4.  
  5.  
  6.  
  7.  
  8.  
  9.   function shoot(index,power) {
  10.  
  11.     if(!(target[index] === undefined)) {
  12.         let ind = target[index]
  13.         ind = ind - power
  14.  
  15.         if(ind <= 0) {
  16.             target.splice(index,1)
  17.         } else {
  18.             target.splice(index,1,ind)
  19.         }
  20.  
  21.     }
  22.    
  23.    
  24.  
  25.   }
  26.  
  27.   function add(index,value) {
  28.     if(!(target[index] === undefined)) {
  29.        
  30.        
  31.         target.splice(index,0,value)
  32.     } else {
  33.         console.log(`Invalid placement!`)
  34.     }
  35.    
  36.   }
  37.  
  38.   function strike(index,radius) {
  39.  
  40.     if(!(target[index] === undefined))  {
  41.                
  42.             if((index + radius) <= target.length-1 && (index-radius !== undefined)) {
  43.                 target.splice((index-radius),(radius*2)+1)
  44.             } else {
  45.                 console.log(`Strike missed!`);              
  46.             }
  47.        
  48.        
  49.     }
  50.    
  51.   }
  52.  
  53.  
  54.  
  55.  
  56.     let token = input.shift()
  57.  
  58.     while(token !== `End`) {
  59.  
  60.     let currentComand =  token.split(` `)
  61.  
  62.     let comand = currentComand[0]
  63.     let indexx =Number(currentComand[1])
  64.     let value = Number(currentComand[2])
  65.  
  66.     if(comand === `Shoot`) {
  67.         shoot(indexx,value);
  68.     } else if(comand === `Add`) {
  69.         add(indexx,value);
  70.     } else if(comand === `Strike`) {
  71.         strike(indexx,value);
  72.     }
  73.    
  74.        
  75.  
  76.     token = input.shift();
  77.   }
  78.  
  79.   console.log(target.join(`|`));
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. }
  89. movingTarget([
  90.     '52 74 23 44 96 110',
  91.     'Shoot 5 10',
  92.     'Shoot 1 80',
  93.     'Strike 2 1',
  94.     'Add 22 3',
  95.     'End'
  96.   ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement