# How to write a javascript formula for the time to clear debt on a credit card calculator?

I have a web developer creating a credit card calculator in Javascript and need a formula to use for calculating the time to clear debt based on inputs of balance, APR, minimum repayment per month (%) and minimum repayment per month (£).

I currently have the below formula but need to adjust it to account for the check to see which minimum repayment to use (£ or %) to occur every month rather than just at the start of the calculation.

We are trying to replicate the minimum repayment calculators seen on MSE and Which.

$$a$$ = credit card balance

$$b$$ = repayment amount per month

$$c$$ = credit card APR

$$e$$ = current credit card monthly rate

$$h$$ = total interest paid

$$k$$ = minimum % required per month

$$m$$ = minimum amount required per month

$$p$$ = amount payable based on %

$$q$$ = time to clear debt

$$e$$ = $$(\text{POWER}(((\frac{c}{100}+1),0.08333)-1) \times 100)\%$$

$$p = k \times a$$

Formula for time to clear debt (q):

IF

$$p > m, q = \frac{\log (\frac{-p}{ea-p})}{\log(1+e)}, q = \frac{log (\frac{-m}{ea-m})}{log(1+e)}$$

• First of all, state the country as rules and regulations (eg on min payment max interest etc) can change. It seems to me you are making some confusion. The minimum repayment is typically the minimum between a fixed amount and a % of the balance. It seems you are trying to build a calculator not for the minimum repayment, but for how long it will take to cler the debt, is that right? If so, please clarify. If not, I don't follow. – Pythonista anonymous Feb 16 at 22:55
• Yes, apologies, it is to calculate the time to clear the debt. This is for the UK. – Andrew Maidstone Feb 17 at 8:37
• Then please update the title and the description of your question accordingly. How did you derive the formula you presented? Do you need a closed-form formula? I am not sure if one exists, but it will be fairly easy to find the solution with a loop. What language do you need to code this in? Java / Javascript? Again, please specify this in your question. – Pythonista anonymous Feb 17 at 9:03
• Thank you, I appreciate the feedback. I need to code this in Javascript so I believe a loop formula would work? The formula was adapted from an old formula used for another part of our website which hasn't really helped with our understanding! – Andrew Maidstone Feb 17 at 9:20

I typed this toy example in Python before you confirmed you need Javascript but, to be honest, it's fairly banal to port it to any other language.

It's just a toy example, which needs to be tidied up and made into a function, but I'm hoping it can be a starting point.

Note I am assuming that the rate remains constant throughout.

You can of course tweak and perfect the example in many ways. In Python you would probably want to use the decimal class - not sure in Javascript.

import pandas as pd
import numpy as np

df = pd.DataFrame(columns = ['balance bop',
'actual payment','interest due','interest paid','interest unpaid','amortisation',
'balance eop'], index = np.arange(0,500), data =0)

df.loc[0,'balance eop'] = 10e3
rate = 1e-2 # ie 12% per year
min_p = 150

for r in np.arange(1,df.shape):
df.loc[r,'balance bop'] = df.loc[r-1,'balance eop']
df.loc[r,'actual payment'] = min( df.loc[r,'balance bop'], min_p)
available = df.loc[r,'actual payment']
df.loc[r,'interest due'] = df.loc[r-1,'interest unpaid'] * (1 + rate) + df.loc[r,'balance bop'] * rate
df.loc[r,'interest paid'] = min(available , df.loc[r,'interest due']   )
df.loc[r,'interest unpaid'] = df.loc[r,'interest due'] - df.loc[r,'interest paid']
available -= df.loc[r,'interest paid']
df.loc[r,'amortisation'] = min( df.loc[r,'balance bop' ], available )
df.loc[r,'balance eop'] = round(df.loc[r,'balance bop'] - df.loc[r,'amortisation'], 3)