Advertisement
Guest User

Untitled

a guest
Apr 27th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function shop(inputData) {
  2.  
  3.   let budget = Number(inputData.shift());
  4.   let nameOfProduct = inputData.shift();
  5.  
  6.   let curProduct = 0;
  7.   let allPrice = 0;
  8.  
  9.   while (nameOfProduct !== 'Stop') {
  10.     let priceOfProduct = Number(inputData.shift());
  11.  
  12.     if ((curProduct + 1) % 3 === 0) {
  13.       priceOfProduct = priceOfProduct / 2;
  14.     }
  15.  
  16.     budget -= priceOfProduct;
  17.     if (budget < 0) {
  18.       break;
  19.     }
  20.  
  21.     curProduct++;
  22.     allPrice += priceOfProduct;
  23.     nameOfProduct = inputData.shift();
  24.   }
  25.  
  26.   if (budget >= 0) {
  27.     console.log(`You bought ${curProduct} products for ${(allPrice).toFixed(2)} leva.`);
  28.   } else if (budget < 0) {
  29.     console.log(`You don't have enough money!`);
  30.    console.log(`You need ${Math.abs(budget).toFixed(2)} leva!`);
  31.  }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement