Guest User

Untitled

a guest
Jul 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. discountPrice2(totalPrice) {
  2. let percentage = 0;
  3. if (totalPrice >= 1000 && totalPrice < 5000) {
  4. percentage = 3;
  5. } else if (totalPrice >= 5000 && totalPrice < 7000) {
  6. percentage = 5;
  7. } else if (totalPrice >= 7000 && totalPrice < 10000) {
  8. percentage = 7;
  9. } else if (totalPrice >= 10000 && totalPrice < 50000) {
  10. percentage = 10;
  11. } else if (totalPrice >= 50000) {
  12. percentage = 15;
  13. } else {
  14. percentage = 0;
  15. }
  16. return this.subtractPercentage(totalPrice, percentage);
  17. }
  18.  
  19. discountPrice(totalPrice) {
  20. const ranges = [
  21. [1000, 5000, 3],
  22. [5000, 7000, 5],
  23. [7000, 10000, 7],
  24. [10000, 50000, 10],
  25. [50000, Number.MAX_SAFE_INTEGER, 15]
  26. ];
  27.  
  28. for (let i = 0; i < ranges.length; i++) {
  29. if (totalPrice < 1000) {
  30. return this.subtractPercentage(totalPrice, 0);
  31. } else if (totalPrice >= ranges[i][0] && totalPrice <= ranges[i][1]) {
  32. return this.subtractPercentage(totalPrice, ranges[i][2]);
  33. break;
  34. }
  35. }
  36. }
  37.  
  38. subtractPercentage(number, percentage) {
  39. number = parseFloat(number);
  40. return number - number / 100 * percentage;
  41. }
Add Comment
Please, Sign In to add comment