Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let allEnergy = Number(input.shift());
  3.     let countWonBattles = 0;
  4.  
  5.     for (let i = 0; i < input.length; i++) {
  6.         let enemy = input[i];
  7.         if (enemy === 'End of battle' && allEnergy > 0) {
  8.             console.log(`Won battles: ${countWonBattles}. Energy left: ${allEnergy}`);
  9.             break;
  10.         }
  11.  
  12.         if (i === input.length - 1 && allEnergy > 0) {
  13.             console.log(`Won battles: ${countWonBattles}. Energy left: ${allEnergy}`);
  14.             break;
  15.         }
  16.  
  17.         if (i % 3 === 0) {
  18.             allEnergy -= Number(enemy)
  19.             allEnergy += countWonBattles;
  20.             countWonBattles++
  21.             if (allEnergy <= 0) {
  22.             console.log(`Not enough energy! Game ends with ${countWonBattles} won battles and ${allEnergy} energy`);
  23.             break;
  24.         } else {
  25.             continue;
  26.         }
  27.         }
  28.         countWonBattles++
  29.         allEnergy -= Number(enemy)
  30.  
  31.         if (allEnergy <= 0) {
  32.             console.log(`Not enough energy! Game ends with ${countWonBattles} won battles and ${allEnergy} energy`);
  33.             break;
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement