Created
February 26, 2016 23:32
-
-
Save Birch-san/5ad240c87ec561e07f77 to your computer and use it in GitHub Desktop.
Compound interest where interest is calculated daily, but credited monthly.
This file contains 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
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