-Enigmos-

touristShop.js

Oct 24th, 2021 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function touristShop(input) {
  2.     let index = 0;
  3.     let budget = Number(input[index]);
  4.     index++;
  5.     let command = input[index];
  6.     index++;
  7.     let productCounter = 0;
  8.     let totalCost = 0;
  9.     let canPay = true;
  10.     let havePurchase = false;
  11.  
  12.     while (command !== "Stop") {
  13.         if (command === undefined) {
  14.             break;
  15.         }
  16.         let productPrice = Number(input[index]);
  17.         index++;
  18.         productCounter++;
  19.         havePurchase = true;
  20.  
  21.         if (productCounter % 3 === 0) {
  22.             productPrice /= 2;
  23.         }
  24.  
  25.         totalCost += productPrice;
  26.         budget -= productPrice;
  27.  
  28.         if (budget < 0) {
  29.             canPay = false;
  30.             console.log(`You don't have enough money!`);
  31.            console.log(`You need ${Math.abs(budget).toFixed(2)} leva!`);
  32.            break;
  33.        }
  34.  
  35.        command = input[index];
  36.        index++;
  37.    }
  38.    if (canPay && havePurchase) {
  39.        console.log(`You bought ${productCounter} products for ${totalCost.toFixed(2)} leva.`);
  40.    }
  41. }
  42.  
  43. touristShop(["153.20", "Backpack", "25.20", "Shoes", "54", "Sunglasses", "30"])
  44. touristShop(["54", "Thermal underwear", "24", "Sunscreen", "45"]);
Add Comment
Please, Sign In to add comment