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