TZinovieva

Counter-Strike JS Fundamentals

Feb 16th, 2023 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function counterStrike(arr) {
  2.     let initialEnergy = Number(arr[0]);
  3.     let index = 1;
  4.     let command = arr[index];
  5.     index++;
  6.     let battlesWon = 0;
  7.     let hasEnoughEnergy = true;
  8.  
  9.     while (command !== "End of battle") {
  10.         let distance = Number(command);
  11.  
  12.         if (initialEnergy - distance < 0) {
  13.             hasEnoughEnergy = false;
  14.             break;
  15.         }
  16.  
  17.         initialEnergy -= distance;
  18.         battlesWon++;
  19.  
  20.         if (battlesWon % 3 === 0) {
  21.             initialEnergy += battlesWon;
  22.         }
  23.         command = arr[index];
  24.         index++;
  25.     }
  26.  
  27.     if (hasEnoughEnergy) {
  28.         console.log(`Won battles: ${battlesWon}. Energy left: ${initialEnergy}`);
  29.     } else {
  30.         console.log(`Not enough energy! Game ends with ${battlesWon} won battles and ${initialEnergy} energy`);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment