Created
May 6, 2015 21:40
-
-
Save Birch-san/54540da1ab717ec707c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return "£"+(function compound(balanceAtYearBeginning, amountDepositedPerYear, coeff, pow, interestOnly, initialAmount) { | |
return pow // are we on base case or inductive case? | |
? compound( | |
(balanceAtYearBeginning + amountDepositedPerYear) * coeff, // apply year's interest to starting balance plus deposit | |
amountDepositedPerYear, | |
coeff, | |
pow-1, | |
interestOnly, | |
initialAmount || balanceAtYearBeginning | |
) | |
-(interestOnly && amountDepositedPerYear) | |
: balanceAtYearBeginning - (interestOnly | |
? initialAmount | |
: 0) | |
})( | |
1000, // balance with which your account begins (in £) | |
300*12, // amount you expect to deposit annually (in £) | |
1.021, // coefficient of interest (i.e. 2.1% = 1.021) | |
5, // over how many years you are depositing | |
true // show just the interest earned (i.e. subtract amount you deposit) | |
) | |
.toFixed(2); // returns ~£1275.76 (i.e. you accrue £1275.76 in interest alone if you deposit this much every year) |
Added support for monthly compounding and application of basic tax rate:
http://jsfiddle.net/ozyp450o/7/
"£"+(function compound(balanceAtPeriodBeginning, amountDepositedPerMonth, coeff, pow, interestOnly, compoundMonthly, basicRate, initialAmount) {
return pow>0 // are we on base case or inductive case?
? compound(
(balanceAtPeriodBeginning // starting balance
+ amountDepositedPerMonth * (compoundMonthly || 12)) // plus amount deposited
* (((!compoundMonthly*coeff || Math.pow(coeff, 1/12)) // has compounding applied
-1)
* (1-(basicRate||0)) // with basic rate of tax deducted
+1),
amountDepositedPerMonth,
coeff,
pow-(compoundMonthly || 12)/12,
interestOnly,
compoundMonthly,
basicRate,
typeof initialAmount === 'undefined' ? balanceAtPeriodBeginning : initialAmount
)
-(interestOnly && amountDepositedPerMonth * (compoundMonthly || 12))
: balanceAtPeriodBeginning - (interestOnly
? initialAmount
: 0)
})(
1000, // balance with which your account begins (in £)
300, // amount you expect to deposit monthly (in £)
1.06, // annual interest (i.e. 2.1% = 1.021) or AER
1, // over how many years you are depositing
true, // show just the interest earned (i.e. subtract amount you deposit)
true,// whether to compound monthly (true) or annually (false).
0.2 // basic rate tax
)
.toFixed(2); // returns ~£140.18 (i.e. you accrue £140.18 in interest alone if you deposit this much every month for this many years, with this much tax, and with monthly compounding, at this AER, beginning with this much in your account)
Support caps (badly):
http://jsfiddle.net/ozyp450o/10/
"£"+(function compound(balanceAtPeriodBeginning, amountDepositedPerMonth, coeff, pow, interestOnly, compoundMonthly, basicRate, cap, initialAmount) {
return pow>=1/12 // are we on base case or inductive case?
? compound(
(balanceAtPeriodBeginning + amountDepositedPerMonth * (compoundMonthly || 12)) // plus amount deposited
+ Math.min((balanceAtPeriodBeginning + amountDepositedPerMonth * (compoundMonthly || 12)), cap || Infinity) // interest applied is upon capped contents
* (((!compoundMonthly*coeff || Math.pow(coeff, 1/12)) // has compounding applied
-1)
* (1-(basicRate||0)) // with basic rate of tax deducted
+1)
- Math.min((balanceAtPeriodBeginning + amountDepositedPerMonth * (compoundMonthly || 12)), cap || Infinity),
amountDepositedPerMonth,
coeff,
pow-(compoundMonthly || 12)/12,
interestOnly,
compoundMonthly,
basicRate,
cap,
typeof initialAmount === 'undefined' ? balanceAtPeriodBeginning : initialAmount
)
-(interestOnly && amountDepositedPerMonth * (compoundMonthly || 12))
: balanceAtPeriodBeginning - (interestOnly
? initialAmount
: 0)
})(
0, // balance with which your account begins (in £)
500, // amount you expect to deposit monthly (in £)
1.05, // annual interest (i.e. 2.1% = 1.021) or AER
1, // over how many years you are depositing
true, // show just the interest earned (i.e. subtract amount you deposit)
true,// whether to compound monthly (true) or annually (false).
0.2, // basic rate tax
2000 // maximum amount upon which interest can be earned
)
.toFixed(2); // returns ~£61.95 (i.e. you accrue £61.95 in interest alone if you deposit this much every month for this many years, with this much tax, and with monthly compounding, at this AER, beginning with this much in your account)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsfiddle.net/ozyp450o/3/