// Savings calculator formulas
// Parameters:
//startingAmount
//years
//additionalContributions
//interestRate
//aditionalContributionsPeriod: 1 - per week; 2 - bi-weekly; 3 - per month; 4 - per quarter; 5 - per year
//interestRatePeriod, Compound: 1 - Daily; 2 - monthly; 3 - quarterly; 4 - annually
function getDataForChart(startingAmount, yearsOfInvesting, additionalContributions, interestRate,
aditionalContributionsPeriod, interestRatePeriod) {
// Map to one - letter variables
//var P = startingAmount;
var Y = yearsOfInvesting;
// var c = getAdditionalContributionsPerPeriod(startingAmount, yearsOfInvesting,
// additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);
// var t = getTermsPerPeriod(startingAmount, yearsOfInvesting,
// additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);
// var n = t * Y;
// var r = interestRate / 100 / t; // interestRate [%]
// var z = 1 + r;
var data1 = new Array();
var data2 = new Array();
for (var i = 1; i <= Y; i++) {
// Base amount:
var B = calculatePrincipal(startingAmount, i, additionalContributions, interestRate,
aditionalContributionsPeriod, interestRatePeriod);
data1[i - 1] = [i, B];
// Compound contributions:
var Cc = calculateTotal(startingAmount, i, additionalContributions, interestRate,
aditionalContributionsPeriod, interestRatePeriod);
// var Cc = P * Math.pow(z, i) + c * (Math.pow(z, i + 1) - z) / r;
data2[i - 1] = [i, Cc];
}
var series = new Array();
series[0] = data1;
series[1] = data2;
return series;
}
function calculatePrincipal(startingAmount, yearsOfInvesting, additionalContributions, interestRate,
aditionalContributionsPeriod, interestRatePeriod) {
var P = startingAmount;
var Y = yearsOfInvesting;
var c = getAdditionalContributionsPerPeriod(startingAmount, yearsOfInvesting,
additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);
var t = getTermsPerPeriod(startingAmount, yearsOfInvesting,
additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);
var n = t * Y;
var r = interestRate / 100 / t; // interestRate [%]
var z = 1 + r;
var coeff = 1;
if (aditionalContributionsPeriod == 1) {
coeff = 52;
}
else if (aditionalContributionsPeriod == 2) {
coeff = 26;
}
else if (aditionalContributionsPeriod == 3) {
coeff = 12;
}
else if (aditionalContributionsPeriod == 4) {
coeff = 4;
}
return startingAmount + coeff * additionalContributions * yearsOfInvesting;
}
function calculateTotal(startingAmount, yearsOfInvesting, additionalContributions, interestRate,
aditionalContributionsPeriod, interestRatePeriod) {
var P = startingAmount;
var Y = yearsOfInvesting;
var c = getAdditionalContributionsPerPeriod(startingAmount, yearsOfInvesting,
additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);
var t = getTermsPerPeriod(startingAmount, yearsOfInvesting,
additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);
var n = t * Y;
var r = interestRate / 100 / t; // interestRate [%]
var z = 1 + r;
return P * Math.pow(z, n) + c * (Math.pow(z, n + 1) - z) / r;
}
function getTermsPerPeriod(startingAmount, yearsOfInvesting, additionalContributions, interestRate,
aditionalContributionsPeriod, interestRatePeriod) {
if (interestRatePeriod == 1) {
return 365;
}
else if (interestRatePeriod == 2) {
return 12;
}
else if (interestRatePeriod == 3) {
return 4;
}
else {
return 1;
}
return 1;
}
function getAdditionalContributionsPerPeriod(startingAmount, yearsOfInvesting,
additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod) {
var divideFactor = 1;
if (interestRatePeriod == 1) {
// Additional contributions per day
if (aditionalContributionsPeriod == 1) {
// Week => factor = 7
divideFactor = 7;
}
else if (aditionalContributionsPeriod == 2) {
// Biweekly => factor = 14
divideFactor = 7;
}
else if (aditionalContributionsPeriod == 3) {
// Monthly => factor = 30
divideFactor = 30;
}
else if (aditionalContributionsPeriod == 4) {
// Per quarter => factor = 91
divideFactor = 91;
}
else if (aditionalContributionsPeriod == 5) {
// Per year => factor = 365
divideFactor = 365;
}
}
else if (interestRatePeriod == 2) {
// Additional contributions per month
if (aditionalContributionsPeriod == 1) {
// Week
divideFactor = 1 / 4;
}
else if (aditionalContributionsPeriod == 2) {
// Biweekly
divideFactor = 1 / 2;
}
else if (aditionalContributionsPeriod == 3) {
// Monthly
divideFactor = 1;
}
else if (aditionalContributionsPeriod == 4) {
// Per quarter
divideFactor = 4;
}
else if (aditionalContributionsPeriod == 5) {
// Per year
divideFactor = 12;
}
}
else if (interestRatePeriod == 3) {
// Additional contributions per quarter
if (aditionalContributionsPeriod == 1) {
// Week
divideFactor = 1 / 13;
}
else if (aditionalContributionsPeriod == 2) {
// Biweekly
divideFactor = 1 / 6.5;
}
else if (aditionalContributionsPeriod == 3) {
// Monthly
divideFactor = 1 / 4;
}
else if (aditionalContributionsPeriod == 4) {
// Per quarter
divideFactor = 1;
}
else if (aditionalContributionsPeriod == 5) {
// Per year
divideFactor = 4;
}
}
else if (interestRatePeriod == 4) {
// Additional contributions per year
if (aditionalContributionsPeriod == 1) {
// Week
divideFactor = 1 / 52;
}
else if (aditionalContributionsPeriod == 2) {
// Biweekly
divideFactor = 1 / 26;
}
else if (aditionalContributionsPeriod == 3) {
// Monthly
divideFactor = 1 / 12;
}
else if (aditionalContributionsPeriod == 4) {
// Per quarter
divideFactor = 1 / 4;
}
else if (aditionalContributionsPeriod == 5) {
// Per year
divideFactor = 1;
}
}
return additionalContributions / divideFactor;
}
var dim = days in month;
var eomform = month + '/' + dim + '/'+ year
var eomdate = new Date(eomform);
var eomeow = eomdate.getDay();
if(eommeow == 6)
{
var multiplier = whater var you set the calculated # of weeks to;
}
else
{
var multiplier = whater var you set the calculated # of weeks to -1;
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Placeholder text not disappearing in Contact Form 7 | 2 | 17 | |
Converting dates in JavaScript | 2 | 35 | |
Why doesn't Google directions work? | 6 | 47 | |
Simple Total function not working | 8 | 32 |
Join the community of 500,000 technology professionals and ask your questions.