うーん...これらの両方は、(私はそれが実際にどのように行われるかわからないので、しかし、わからない)可能性が物事が実際に行われているかをより正確にその計算から少しずれていますが、:
function calc(startingAmount, yearsOfInvesting, additionalContributionsPerPeriod, interestRate) {
var interestPerDay = (interestRate/365);
var total = startingAmount;
var date = new Date(new Date().getFullYear() + new Date().getMonth() + 1, 1);
var endDate = new Date(date.getFullYear() + yearsOfInvesting, date.getMonth(), date.getDate() - 1);
var startingWeekday = date.getDay();
var startingDate = date.getDate();
var runningInterest = 0;
while(Date.parse(date.toString()) < Date.parse(endDate.toString())) {
date.setDate(date.getDate() + 1);
runningInterest = runningInterest + total * interestPerDay;
if(date.getDay() == startingWeekday) {
total = total + additionalContributionsPerPeriod;
}
if(date.getDate() == startingDate) {
total = total + runningInterest;
runningInterest = 0;
}
}
total = total + runningInterest;
return total;
}
function calc2(startingAmount, yearsOfInvesting, additionalContributionsPerPeriod, interestRate) {
var interestPerDay = (interestRate/365);
var total = startingAmount;
var runningInterest = 0;
for(var day = 1; day <= 365 * yearsOfInvesting; day++) {
runningInterest = runningInterest + total * interestPerDay;
if(day % 7 == 0) {
total = total + additionalContributionsPerPeriod;
}
if(day % 30 == 0) {
total = total + runningInterest;
runningInterest = 0;
}
}
total = total + runningInterest;
return total;
}
document.write(3647 + "<br>" + calc(1000, 1, 50, 0.02) + "<br>" + calc2(1000, 1, 50, 0.02));
document.write("<br><br>");
document.write(6347 + "<br>" + calc(1000, 2, 50, 0.02) + "<br>" + calc2(1000, 2, 50, 0.02));
document.write("<br><br>");
document.write(14779 + "<br>" + calc(1000, 5, 50, 0.02) + "<br>" + calc2(1000, 5, 50, 0.02));
document.write("<br><br>");
document.write(30007 + "<br>" + calc(1000, 10, 50, 0.02) + "<br>" + calc2(1000, 10, 50, 0.02));
document.write("<br><br>");
document.write(31673 + "<br>" + calc(1000, 10, 50, 0.03) + "<br>" + calc2(1000, 10, 50, 0.03));
document.write("<br><br>");
document.write(33460 + "<br>" + calc(1000, 10, 50, 0.04) + "<br>" + calc2(1000, 10, 50, 0.04));
document.write("<br><br>");
document.write(35378 + "<br>" + calc(1000, 10, 50, 0.05) + "<br>" + calc2(1000, 10, 50, 0.05));
document.write("<br><br>");
document.write(772849953 + "<br>" + calc(1000, 55, 50, 0.20) + "<br>" + calc2(1000, 55, 50, 0.20));