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