Skip to content

Instantly share code, notes, and snippets.

@craftdelivery
Last active April 5, 2020 03:56
Show Gist options
  • Save craftdelivery/aa398c2640aa2e8c2fef7f68999e4754 to your computer and use it in GitHub Desktop.
Save craftdelivery/aa398c2640aa2e8c2fef7f68999e4754 to your computer and use it in GitHub Desktop.
Calculate escalating discount using recursion

calc discount

a recursive function to calculate compounding discount

discount rate: $1.50

gas surcharge: $0.35

stops discount cost per stop
1 $1.50 $8.50 $8.50
2 $3.80 $16.20 $8.10
3 $7.25 $22.75 $7.58
4 $11.85 $28.15 $7.04
5 $17.60 $32.40 $6.48
6 $24.50 $35.50 $5.92
const calc = (stops, discount=1.50, surcharge=0.35) => {
if (stops <= 0) return 0
const gasSurcharge = stops > 1 ? stops * surcharge : 0
return calc(stops - 1, discount, surcharge) + stops * discount - gasSurcharge
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment