Advertisement
Guest User

Problem 1. Experience Gaining

a guest
Dec 7th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     arr = arr.map(Number);
  3.    
  4.     let neededPoints = arr.shift();
  5.     let battles = arr.shift();
  6.     let totalPoints = 0;
  7.    
  8.      let num = 0;
  9.  
  10.     for (let i = 0; i < battles; i++) {
  11.        
  12.         if (i % 3 === 0) {
  13.             totalPoints += (arr[i] + arr[i] * 0.15);
  14.             num++;
  15.         } else if (i % 5 === 0) {
  16.  
  17.             totalPoints += (arr[i] - arr[i] * 0.1);
  18.             num++;
  19.         } else {
  20.  
  21.             totalPoints += arr[i];
  22.             num++;
  23.         }
  24.  
  25.  
  26.         if (totalPoints >= neededPoints || totalPoints === neededPoints) {
  27.             console.log(`Player successfully collected his needed experience for ${num} battles.`);
  28.             return;
  29.         }
  30.     }
  31.  
  32.      
  33.         console.log(`Player was not able to collect the needed experience, ${(neededPoints - totalPoints).toFixed(2)} more needed.`);
  34.  
  35.      
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement