Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let allEnergy = Number(input.shift());
- let countWonBattles = 0;
- for (let i = 0; i < input.length; i++) {
- let enemy = input[i];
- if (enemy === 'End of battle' && allEnergy > 0) {
- console.log(`Won battles: ${countWonBattles}. Energy left: ${allEnergy}`);
- break;
- }
- if (i === input.length - 1 && allEnergy > 0) {
- console.log(`Won battles: ${countWonBattles}. Energy left: ${allEnergy}`);
- break;
- }
- if (i % 3 === 0) {
- allEnergy -= Number(enemy)
- allEnergy += countWonBattles;
- countWonBattles++
- if (allEnergy <= 0) {
- console.log(`Not enough energy! Game ends with ${countWonBattles} won battles and ${allEnergy} energy`);
- break;
- } else {
- continue;
- }
- }
- countWonBattles++
- allEnergy -= Number(enemy)
- if (allEnergy <= 0) {
- console.log(`Not enough energy! Game ends with ${countWonBattles} won battles and ${allEnergy} energy`);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement