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 accessoryCounter = 0;
- let accessoryTotalCost = 0;
- let enoughMoney = true;
- while (command !== "Stop") {
- let accessoryPrice = Number(input[index]);
- index++;
- accessoryCounter++;
- if (accessoryCounter % 3 === 0) {
- accessoryPrice /= 2;
- }
- if (budget - accessoryPrice < 0) {
- enoughMoney = false;
- let requiredMoney = accessoryPrice - budget;
- console.log(`You don't have enough money!`);
- console.log(`You need ${requiredMoney.toFixed(2)} leva!`);
- break;
- } else {
- budget -= accessoryPrice;
- accessoryTotalCost += accessoryPrice;
- }
- command = input[index];
- index++;
- }
- if (enoughMoney) {
- console.log(`You bought ${accessoryCounter} products for ${accessoryTotalCost.toFixed(2)} leva.`);
- }
- }
- touristShop(["153.20", "Backpack", "25.20", "Shoes", "54", "Sunglasses", "30", "Stop"]);
- touristShop(["54", "Thermal underwear", "24", "Sunscreen", "45"]);
Advertisement
Add Comment
Please, Sign In to add comment