嗯......这两个都与那个计算器略有不同,但可能更准确地了解事情的实际完成方式(但是不确定,因为我不知道它实际上是如何完成的):
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 ) );