Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function touristShop(input) {
- let index = 0;
- let budget = Number(input[index]);
- index++;
- let command = input[index];
- index++;
- let productCounter = 0;
- let totalCost = 0;
- let canPay = true;
- let havePurchase = false;
- while (command !== "Stop") {
- if (command === undefined) {
- break;
- }
- let productPrice = Number(input[index]);
- index++;
- productCounter++;
- havePurchase = true;
- if (productCounter % 3 === 0) {
- productPrice /= 2;
- }
- totalCost += productPrice;
- budget -= productPrice;
- if (budget < 0) {
- canPay = false;
- console.log(`You don't have enough money!`);
- console.log(`You need ${Math.abs(budget).toFixed(2)} leva!`);
- break;
- }
- command = input[index];
- index++;
- }
- if (canPay && havePurchase) {
- console.log(`You bought ${productCounter} products for ${totalCost.toFixed(2)} leva.`);
- }
- }
- touristShop(["153.20", "Backpack", "25.20", "Shoes", "54", "Sunglasses", "30"])
- touristShop(["54", "Thermal underwear", "24", "Sunscreen", "45"]);
Add Comment
Please, Sign In to add comment