-Enigmos-

touristShop.js

Nov 24th, 2021
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 accessoryCounter = 0;
  8.     let accessoryTotalCost = 0;
  9.     let enoughMoney = true;
  10.  
  11.     while (command !== "Stop") {
  12.         let accessoryPrice = Number(input[index]);
  13.         index++;
  14.         accessoryCounter++;
  15.  
  16.         if (accessoryCounter % 3 === 0) {
  17.             accessoryPrice /= 2;
  18.         }
  19.  
  20.         if (budget - accessoryPrice < 0) {
  21.             enoughMoney = false;
  22.             let requiredMoney = accessoryPrice - budget;
  23.             console.log(`You don't have enough money!`);
  24.            console.log(`You need ${requiredMoney.toFixed(2)} leva!`);
  25.            break;
  26.        } else {
  27.            budget -= accessoryPrice;
  28.            accessoryTotalCost += accessoryPrice;
  29.        }
  30.  
  31.        command = input[index];
  32.        index++;
  33.    }
  34.  
  35.    if (enoughMoney) {
  36.        console.log(`You bought ${accessoryCounter} products for ${accessoryTotalCost.toFixed(2)} leva.`);
  37.    }
  38. }
  39.  
  40. touristShop(["153.20", "Backpack", "25.20", "Shoes", "54", "Sunglasses", "30", "Stop"]);
  41. touristShop(["54", "Thermal underwear", "24", "Sunscreen", "45"]);
Advertisement
Add Comment
Please, Sign In to add comment