Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function counterStrike(arr) {
- let initialEnergy = Number(arr[0]);
- let index = 1;
- let command = arr[index];
- index++;
- let battlesWon = 0;
- let hasEnoughEnergy = true;
- while (command !== "End of battle") {
- let distance = Number(command);
- if (initialEnergy - distance < 0) {
- hasEnoughEnergy = false;
- break;
- }
- initialEnergy -= distance;
- battlesWon++;
- if (battlesWon % 3 === 0) {
- initialEnergy += battlesWon;
- }
- command = arr[index];
- index++;
- }
- if (hasEnoughEnergy) {
- console.log(`Won battles: ${battlesWon}. Energy left: ${initialEnergy}`);
- } else {
- console.log(`Not enough energy! Game ends with ${battlesWon} won battles and ${initialEnergy} energy`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment