Spocoman

Gold Mine

Jan 6th, 2022 (edited)
135
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.  
  5.     for (let i = 0; i < locations; i++) {
  6.         let averageGold = Number(input[index++]);
  7.         let dayDigging = Number(input[index++]);
  8.         let goldSum = 0;
  9.         for (let j = 0; j < dayDigging; j++) {
  10.             goldSum += Number(input[index++]);
  11.         }
  12.  
  13.         if ((goldSum / dayDigging) >= averageGold) {
  14.             console.log(`Good job! Average gold per day: ${(goldSum / dayDigging).toFixed(2)}.`)
  15.         } else {
  16.             console.log(`You need ${(averageGold - (goldSum / dayDigging)).toFixed(2)} gold.`)
  17.         }
  18.     }
  19. }
  20.  
  21. Решение със shift():
  22.  
  23. function goldMine(input) {
  24.     let locations = Number(input.shift());
  25.  
  26.     for ( let i = 0; i < locations; i++) {
  27.         let averageGold = Number(input.shift());
  28.         let dayDigging = Number(input.shift());
  29.         let goldSum = 0;
  30.         for (let j = 0; j < dayDigging; j++) {
  31.             goldSum += Number(input.shift());
  32.         }
  33.        
  34.         if ((goldSum / dayDigging) >= averageGold) {
  35.             console.log(`Good job! Average gold per day: ${(goldSum / dayDigging).toFixed(2)}.`)
  36.         } else {
  37.             console.log(`You need ${(averageGold - (goldSum / dayDigging)).toFixed(2)} gold.`)
  38.         }
  39.     }
  40. }
  41.  
Add Comment
Please, Sign In to add comment