Advertisement
Guest User

Untitled

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