Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
263
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.         }
  12.         if(initialEnergy >= parseInt(input[i])) {
  13.             initialEnergy = initialEnergy - parseInt(input[i]);
  14.             counter++;
  15.             if(counter % 3 == 0) {
  16.                 initialEnergy = initialEnergy + counter;
  17.             }
  18.         } else {
  19.             console.log(`Not enough energy! Game ends with ${counter} won battles and ${initialEnergy} energy`)
  20.             break;
  21.         }
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement