Skip to content

Instantly share code, notes, and snippets.

@Birch-san
Created February 26, 2016 23:32
Show Gist options
  • Save Birch-san/5ad240c87ec561e07f77 to your computer and use it in GitHub Desktop.
Save Birch-san/5ad240c87ec561e07f77 to your computer and use it in GitHub Desktop.
Compound interest where interest is calculated daily, but credited monthly.
var finalAmount = (function() {
var initialPounds = 40000;
var denominationOfYearUponWhichInterestCalculated = 365;
var denominationsAfterWhichInterestCredited = Math.floor(denominationOfYearUponWhichInterestCalculated/12);
var annnualInterestCoefficient = 1.05;
var denominationInterestCoefficient = Math.pow(annnualInterestCoefficient, 1/denominationOfYearUponWhichInterestCalculated);
var yearsSimulated = 5;
var amountSpentPerMonth = 500;
var amountSpentPerYear = amountSpentPerMonth*12;
var amountSpentPerDenomination = amountSpentPerYear/denominationOfYearUponWhichInterestCalculated;
var currentAmount = initialPounds;
var i;
var j;
var denominationsPassed = 0;
var interestWaitingToBeCredited = 0;
for(i=0; i<yearsSimulated; i++) {
console.log(currentAmount.toFixed(2));
for(j = 0; j<denominationOfYearUponWhichInterestCalculated; j++) {
// console.log(currentAmount.toFixed(2));
var interestEarnedDuringThisDenomination = (currentAmount+interestWaitingToBeCredited)*denominationInterestCoefficient-(currentAmount+interestWaitingToBeCredited);
interestWaitingToBeCredited += interestEarnedDuringThisDenomination;
currentAmount -= amountSpentPerDenomination;
denominationsPassed++;
if (denominationsPassed%denominationsAfterWhichInterestCredited == 0) {
currentAmount += interestWaitingToBeCredited;
interestWaitingToBeCredited = 0;
}
}
}
return currentAmount;
})();
console.log(finalAmount.toFixed(2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment