Advertisement
Kuzmov

Untitled

Dec 14th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. function solve(input) {
  2. let budget = +input.shift();
  3. let product = input.shift();
  4. let price = 0;
  5. let productsCounter = 0;
  6. let moneySpent = 0;
  7. let boughtProducts = 0;
  8.  
  9. while (product !== 'Finish') {
  10. productsCounter++;
  11. if (product == 'Star') {
  12. price = 5.69;
  13. } else if (product == 'Angel') {
  14. price = 8.49;
  15. } else if (product == 'Lights') {
  16. price = 11.20;
  17. } else if (product == 'Wreath') {
  18. price = 15.50;
  19. } else if (product == 'Candle') {
  20. price = 3.59;
  21. }
  22. if (productsCounter % 3 == 0) {
  23. price = 0.7;
  24. }
  25.  
  26. budget -= price;
  27. moneySpent += price;
  28.  
  29. if (budget price) {
  30. let diff = price - budget;
  31. console.log(`Not enough money! You need ${diff} lv more.`);
  32. break;
  33. }
  34. boughtProducts++;
  35. product = input.shift();
  36. if (product == 'Finish') {
  37. console.log(`Congratulations! You bought everything!`);
  38. break;
  39. }
  40. }
  41.  
  42. console.log(`${boughtProducts} items - ${moneySpent.toFixed(2)} lv spent.`);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement