-Enigmos-

goldMine.js

Oct 31st, 2021 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function goldMine(input) {
  2.     let index = 0;
  3.     let locations = Number(input[index]);
  4.     index++;
  5.  
  6.     for (let i = 0; i < locations; i++) {
  7.         let avgProfitGoal = Number(input[index]);
  8.         index++;
  9.         let days = Number(input[index]);
  10.         index++;
  11.         let profitSum = 0;
  12.        
  13.         for (let day = 0; day < days; day++) {
  14.             let currentProfit = Number(input[index]);
  15.             index++;
  16.             profitSum += currentProfit;
  17.         }
  18.  
  19.         let avgProfit = profitSum / days;
  20.  
  21.         if (avgProfit >= avgProfitGoal) {
  22.             console.log(`Good job! Average gold per day: ${avgProfit.toFixed(2)}.`);
  23.         } else {
  24.             console.log(`You need ${(avgProfitGoal - avgProfit).toFixed(2)} gold.`);
  25.         }
  26.     }
  27.  
  28. }
Add Comment
Please, Sign In to add comment