Advertisement
Spocoman

Club

Jan 7th, 2022 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function club(input) {
  2.     let neededSum = Number(input[0]);
  3.     let profit = 0;
  4.  
  5.     for (let i = 1; i < (input.length || profit >= neededSum); i++) {
  6.         let drink = input[i];
  7.         if (drink === "Party!") {
  8.             break;
  9.         }
  10.         let sum = drink.length * Number(input[++i]);
  11.         if (sum % 2 === 1) {
  12.             sum *= 0.75;
  13.         }
  14.         profit += sum;
  15.     }
  16.  
  17.     if (neededSum > profit) {
  18.         console.log(`We need ${(neededSum - profit).toFixed(2)} leva more.`);
  19.     } else {
  20.         console.log("Target acquired.");
  21.     }
  22.     console.log(`Club income - ${profit.toFixed(2)} leva.`);
  23. }
  24.  
  25. РЕШЕНИЕ С WHILE И SHIFT():
  26.  
  27. function club(input) {
  28.     let neededSum = Number(input.shift());
  29.     let drink = input.shift();
  30.     let profit = 0;
  31.    
  32.     while (drink !== "Party!" && profit < neededSum) {
  33.         let sum = drink.length * Number(input.shift());
  34.         if (sum % 2 === 1) {
  35.             sum *= 0.75;
  36.         }
  37.         profit += sum;
  38.         drink = input.shift();
  39.     }
  40.    
  41.     if (neededSum > profit) {
  42.         console.log(`We need ${(neededSum - profit).toFixed(2)} leva more.`);
  43.     } else {
  44.         console.log("Target acquired.");
  45.     }
  46.     console.log(`Club income - ${profit.toFixed(2)} leva.`);    
  47. }
  48.  
  49. И С ТЕРНАРЕН ОПЕРАТОР:
  50.  
  51. function club(input) {
  52.     let neededSum = Number(input.shift());
  53.     let drink = input.shift();
  54.     let profit = 0;
  55.  
  56.     while (drink !== "Party!" && profit < neededSum) {
  57.         let sum = drink.length * Number(input.shift());
  58.         sum *= sum % 2 === 1 ? 0.75 : 1;
  59.         profit += sum;
  60.         drink = input.shift();
  61.     }
  62.  
  63.     console.log(neededSum <= profit ? "Target acquired." :
  64.         `We need ${(neededSum - profit).toFixed(2)} leva more.`);
  65.     console.log(`Club income - ${profit.toFixed(2)} leva.`);
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement