Advertisement
Spocoman

Tourist Shop

Feb 17th, 2022 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function touristShop(input) {
  2.     let i = 0;
  3.     let sum = 0;
  4.     let counter = 0;
  5.     let budget = Number(input[i++]);
  6.     let command = input[i++];
  7.  
  8.     while (command !== "Stop" && sum <= budget) {
  9.         let price = Number(input[i++]);
  10.         counter++;
  11.         if (counter % 3 === 0) {
  12.             price /= 2;
  13.         }
  14.         sum += price;
  15.         command = input[i++];
  16.     }
  17.  
  18.     if (command === "Stop") {
  19.         console.log(`You bought ${counter} products for ${sum.toFixed(2)} leva.`);
  20.     } else {
  21.         console.log("You don't have enough money!")
  22.         console.log(`You need ${(sum - budget).toFixed(2)} leva!`)
  23.     }
  24. }
  25.  
  26. РЕШЕНИЕ СЪС SHIFT():
  27.  
  28. function touristShop(input) {
  29.     let sum = 0;
  30.     let counter = 0;
  31.     let budget = Number(input.shift());
  32.     let command = input.shift();
  33.  
  34.     while (command !== "Stop" && sum <= budget) {
  35.         let price = Number(input.shift());
  36.         counter++;
  37.         if (counter % 3 === 0) {
  38.             price /= 2;
  39.         }
  40.         sum += price;
  41.         command = input.shift();
  42.     }
  43.  
  44.     if (command === "Stop") {
  45.         console.log(`You bought ${counter} products for ${sum.toFixed(2)} leva.`);
  46.     } else {
  47.         console.log("You don't have enough money!")
  48.         console.log(`You need ${(sum - budget).toFixed(2)} leva!`)
  49.     }
  50. }
  51.  
  52. РЕШЕНИЕ С FOR:
  53.  
  54. function touristShop(input) {
  55.     let sum = 0;
  56.     let budget = Number(input[0]);
  57.  
  58.     for (let i = 1; i < input.length; i++) {
  59.         let command = input[i];
  60.         if (command === "Stop") {
  61.             console.log(`You bought ${parseInt(i / 2)} products for ${sum.toFixed(2)} leva.`);
  62.             break;
  63.         }
  64.         let priceProduct = Number(input[++i]);
  65.         if (i % 3 === 0) {
  66.             priceProduct /= 2;
  67.         }
  68.         sum += priceProduct;
  69.        
  70.         if (sum > budget) {
  71.             console.log("You don't have enough money!");
  72.             console.log(`You need ${(sum - budget).toFixed(2)} leva!`);
  73.             break;
  74.         }
  75.     }
  76. }
  77.  
  78. И ЛЕКО ТАРИКАТСКАТА СЪС SHIFT() И ТЕРНАРЕН ОПЕРАТОР:
  79.  
  80. function touristShop(input) {
  81.     let sum = 0;
  82.     let budget = Number(input.shift());
  83.  
  84.     for (let i = 1; i < Number.MAX_SAFE_INTEGER; i++) {
  85.         if (input.shift() === "Stop") {
  86.             console.log(`You bought ${--i} products for ${sum.toFixed(2)} leva.`);
  87.             break;
  88.         }
  89.  
  90.         sum += Number(input.shift()) * (i % 3 === 0 ? 0.5 : 1);
  91.  
  92.         if (sum > budget) {
  93.             console.log(`You don't have enough money!\nYou need ${(sum - budget).toFixed(2)} leva!`);
  94.            break;
  95.        }
  96.    }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement