vladovip

CounterStrike_MidExamPrep_JS Fund

Feb 21st, 2022 (edited)
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Judge gives 100/100
  2.  
  3. function counterStrike(inputArr) {
  4.  
  5.     let initialEnergy = Number(inputArr.shift());
  6.     // console.log(initialEnergy);
  7.     let counterForBattlesWin = 0;
  8.     let arrayOfCommands = inputArr;
  9.     let currentCommand = arrayOfCommands.shift();
  10.  
  11.     while (currentCommand !== "End of battle" ) {
  12.    
  13.          currentCommand = Number(currentCommand);
  14.          
  15.          if ( initialEnergy < currentCommand){
  16.             console.log(`Not enough energy! Game ends with ${counterForBattlesWin} won battles and ${initialEnergy} energy`);
  17.             initialEnergy -= currentCommand;
  18.             break;
  19.          }
  20.          
  21.          initialEnergy -= currentCommand;
  22.          counterForBattlesWin++;
  23.  
  24.       if(counterForBattlesWin % 3 == 0  ){
  25.           initialEnergy += counterForBattlesWin
  26.       }
  27.       currentCommand = arrayOfCommands.shift();
  28.     }
  29.    
  30.     if (initialEnergy >= 0){
  31.       console.log(`Won battles: ${counterForBattlesWin}. Energy left: ${initialEnergy}`);
  32.       }
  33.      
  34.   }
  35.  
  36.   counterStrike(["100", "10", "10", "10", "1", "2", "3", "73", "10"]);
  37.   counterStrike(["200","54","14","28","13","End of battle"]);
  38.  
Add Comment
Please, Sign In to add comment