GeriZlateva

counterStrike

Jun 23rd, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let initialEnergy = Number(input.shift());
  3.     let currentDistance = Number(input.shift());
  4.     let battlesCount = 0;
  5.     let wonCount = 0;
  6.     let finished = true;
  7.  
  8.     while (currentDistance !== 'End of battle') {
  9.         currentDistance = Number(currentDistance);
  10.  
  11.         if (initialEnergy >= currentDistance) {
  12.             initialEnergy -= currentDistance;
  13.             wonCount++;
  14.         } else {
  15.             battlesCount++;
  16.             console.log(`Not enough energy! Game ends with ${wonCount} won battles and ${initialEnergy} energy`);
  17.             finished = false;
  18.             break;
  19.         }
  20.  
  21.         if (wonCount % 3 === 0) {
  22.             initialEnergy += wonCount;
  23.         }
  24.         currentDistance = input.shift();
  25.     }
  26.  
  27.     if (finished) {
  28.         console.log(`Won battles: ${wonCount}. Energy left: ${initialEnergy}`);
  29.     }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment