Advertisement
Spocoman

11. Bitcoin "Mining"

Jan 13th, 2022 (edited)
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. РЕШЕНИЕ С FOR:
  2.  
  3. function bitcoin(input) {
  4.     let sum = 0;
  5.     let firstBuy = 0;
  6.  
  7.     for (let i = 1; i <= input.length; i++) {
  8.         let currentSum = input[i - 1] * 67.51;
  9.  
  10.         if (i % 3 === 0) {
  11.             currentSum *= 0.7;
  12.         }
  13.         sum += currentSum;
  14.  
  15.         if (firstBuy === 0 && sum >= 11949.16) {
  16.             firstBuy = i;
  17.         }
  18.     }
  19.  
  20.     let bitcoins = Math.floor(sum / 11949.16);
  21.     sum %= 11949.16;
  22.  
  23.     console.log(`Bought bitcoins: ${bitcoins}`);
  24.     if (bitcoins !== 0) {
  25.         console.log(`Day of the first purchased bitcoin: ${firstBuy}`);
  26.     }
  27.     console.log(`Left money: ${sum.toFixed(2)} lv.`);
  28. }
  29.  
  30. РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
  31.  
  32. function bitcoin(input) {
  33.     let sum = 0;
  34.     let firstBuy = 0;
  35.  
  36.     for (let i = 1; i <= input.length; i++) {
  37.         sum += input[i - 1] * 67.51 * (i % 3 === 0 ? 0.7 : 1);
  38.         firstBuy += firstBuy === 0 && sum >= 11949.16 ? i : 0;
  39.     }
  40.  
  41.     console.log(`Bought bitcoins: ${parseInt(sum / 11949.16)}\n${parseInt(sum / 11949.16) !== 0 ?
  42.      `Day of the first purchased bitcoin: ${firstBuy}\n` : ''}Left money: ${(sum % 11949.16).toFixed(2)} lv.`);  
  43. }
  44.  
  45. РЕШЕНИЕ С WHILE, SHIFT() И ТЕРНАРЕН ОПЕРАТОР:
  46.  
  47. function bitcoin(input) {
  48.     let sum = 0;
  49.     let firstBuy = 0;
  50.     let i = 1;
  51.  
  52.     while (input.length !== 0) {
  53.         sum += input.shift() * 67.51 * (i % 3 === 0 ? 0.7 : 1);
  54.         firstBuy += firstBuy === 0 && sum >= 11949.16 ? i : 0;
  55.         i++;
  56.     }
  57.  
  58.     console.log(`Bought bitcoins: ${parseInt(sum / 11949.16)}\n${parseInt(sum / 11949.16) !== 0 ?
  59.      `Day of the first purchased bitcoin: ${firstBuy}\n` : ''}Left money: ${(sum % 11949.16).toFixed(2)} lv.`);  
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement