ErolKZ

Untitled

Jul 2nd, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4.  
  5.  
  6. let budged = Number(input[0]);
  7.  
  8. let countBuyedProducts = 0;
  9. let priceOfPurchases = 0;
  10.  
  11.  
  12. for (let i = 1; i < input.length; i = i + 2) {
  13.  
  14.  
  15. if (input[i] !== 'Stop') {
  16.  
  17.  
  18. let currentNumber = Number(input[i + 1]);
  19.  
  20. budged -= currentNumber;
  21.  
  22. if (budged <= 0) {
  23.  
  24. budged = Math.abs(budged);
  25.  
  26. console.log(`You don't have enough money!`);
  27. console.log(`You need ${budged.toFixed(2)} leva!`);
  28.  
  29. break;
  30.  
  31. }
  32.  
  33. countBuyedProducts += 1;
  34.  
  35. if (countBuyedProducts % 3 === 0) {
  36.  
  37. currentNumber = currentNumber / 2;
  38.  
  39. }
  40.  
  41. priceOfPurchases += currentNumber;
  42.  
  43.  
  44.  
  45. } else {
  46.  
  47.  
  48. console.log(`You bought ${countBuyedProducts} products for ${priceOfPurchases.toFixed(2)} leva.`);
  49.  
  50.  
  51. break;
  52.  
  53. }
  54.  
  55.  
  56. } // For Loop
  57.  
  58.  
  59.  
  60.  
  61.  
  62. }
  63.  
  64.  
  65. solve([
  66.  
  67. '150',
  68. 'Backpack',
  69. '25.20',
  70. 'Shoes',
  71. '54',
  72. 'Sunglasses',
  73. '30',
  74. 'Backpack',
  75. '25.20',
  76. 'Shoes',
  77. '54',
  78. 'Sunglasses',
  79. '30',
  80. 'Stop'
  81.  
  82. // '54',
  83. // 'Thermal underwear',
  84. // '24',
  85. // 'Sunscreen',
  86. // '45'
  87.  
  88. ]);
Advertisement
Add Comment
Please, Sign In to add comment