Advertisement
desito07

Report System

Apr 17th, 2020
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. function reportSystem(input) {
  2. let requiredSum = Number(input.shift());
  3.  
  4. let expectedSum = 0;
  5. let counter = 1;
  6. let cardTransactionSum = 0;
  7. let cashTransactionSum = 0;
  8. let cardCount = 0;
  9. let cashCount = 0;
  10.  
  11. while (true) {
  12. let command = input.shift();
  13. if (command === "End") {
  14. break;
  15. }
  16. if (counter % 2 === 0) {
  17. expectedSum += cardTransactionSum;
  18. } else {
  19. expectedSum += cashTransactionSum;
  20. }
  21.  
  22. if (expectedSum > requiredSum) {
  23. break;
  24. }
  25.  
  26. if (counter % 2 === 0) {
  27. if (command < 10) {
  28. console.log("Error in transaction!");
  29. } else {
  30. console.log("Product sold!");
  31. cardTransactionSum += Number(command);
  32. cardCount++;
  33. }
  34. } else {
  35. if (command > 100) {
  36. console.log("Error in transaction!");
  37. } else {
  38. console.log("Product sold!");
  39. cashTransactionSum += Number(command);
  40. cashCount++;
  41. }
  42. }
  43. counter++;
  44. }
  45. if (expectedSum >= requiredSum) {
  46. let avrCash = cashTransactionSum / cashCount;
  47. let avrCard = cardTransactionSum / cardCount;
  48. console.log(`Average CS: ${avrCash.toFixed(2)}`);
  49. console.log(`Average CC: ${avrCard.toFixed(2)}`);
  50. } else {
  51. console.log("Failed to collect required money for charity.");
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement