home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 November
/
pcwk_11_98a.iso
/
Wtestowe
/
Money99
/
money99.1
/
savapp
/
logic.js
< prev
next >
Wrap
Text File
|
1998-07-25
|
7KB
|
249 lines
// This function calculates annual interest rate income on monthly
// compound basis.
function annualDeposit(balance, interest)
{
var numMon = 12
var retBal = balance
var monthlyInt = interest / numMon
while(numMon > 0) { // do the monthly compounding
retBal = retBal * (1 + monthlyInt)
numMon = numMon - 1
}
retBal = retBal - balance // get net interest rate income
return retBal
}
// This function calculates annual interest rate income on monthly
// compound and quarterly deposit basis.
function quarterDeposit(balance, interest, amount)
{
var retBal = balance
var monthlyInt = interest / 12 // monthly interest
var i, j
i = 0
while(i < 4) { // quarterly loop
j = 0
while(j < 3) { // monthly loop
retBal = retBal * (1 + monthlyInt)
j = j + 1
}
i = i + 1
retBal = retBal + amount // add the quarterly deposit
}
retBal = retBal - (balance + amount * 4) // net interest rate income
return retBal
}
// This function calculates annual interest rate income on monthly
// compound and monthly deposit basis.
function monthDeposit(balance, interest, amount)
{
var retBal = balance
var monthlyInt = interest / 12 // monthly interest
var i
i = 0
while(i < 12) {
retBal = retBal * (1 + monthlyInt)
retBal = retBal + amount
i = i + 1
}
retBal = retBal - (balance + amount * 12)
return retBal
}
// This function calculates all the values for the result page.
function getResultSet1()
{
var sto = parent.data
var numYear
var prevBal, totalTax, totalInterest, yearlyRet, yearlyDep
prevBal = sto.deposit_balance
totalTax = 0
totalInterest = 0
numYear = sto.deposit_term
if(sto.deposit_freq == 0) // yearly deposit
sto.result_deposit = sto.deposit_regular * numYear
else if(sto.deposit_freq == 1) // quarterly deposit
sto.result_deposit = sto.deposit_regular * numYear * 4
else if(sto.deposit_freq == 2) // monthly deposit
sto.result_deposit = sto.deposit_regular * numYear * 12
sto.result_deposit = Math.round(sto.result_deposit)
for(i=0; i<numYear; i++) {
if(sto.deposit_freq == 0) { // deposit annually
yearlyRet = annualDeposit(prevBal, sto.interest_rate)
yearlyDep = sto.deposit_regular
}
else if(sto.deposit_freq == 1) { // deposit quarterly
yearlyRet = quarterDeposit(prevBal, sto.interest_rate, sto.deposit_regular)
yearlyDep = sto.deposit_regular * 4
}
else if(sto.deposit_freq == 2) { // deposit monthly
yearlyRet = monthDeposit(prevBal, sto.interest_rate, sto.deposit_regular)
yearlyDep = sto.deposit_regular * 12
}
// interest earning is pre-taxed.
totalInterest = totalInterest + yearlyRet
// net saving is either taxed or not
if(sto.taxrate_istaxed == 1) {
totalTax = totalTax + yearlyRet * sto.taxrate_taxrate
prevBal = prevBal + yearlyRet * ( 1 - sto.taxrate_taxrate)
}
else
prevBal = prevBal + yearlyRet
prevBal += yearlyDep
}
sto.result_saved = Math.round(prevBal)
sto.result_interest = Math.round(totalInterest)
sto.result_tax = Math.round(totalTax)
sto.result_term = sto.deposit_term * 12
sto.result_regular = sto.deposit_regular
}
function getResultSet2(goal)
{
var sto = parent.data
var numMonth, numYear, numQuarter
var prevBal, totalTax, totalInterest
var monthlyRet, monthlyDep, cumMonthlyRet
prevBal = sto.deposit_balance
totalTax = totalInterest = numMonth = cumMonthlyRet = 0
while(1) {
monthlyRet = prevBal * sto.interest_rate / 12
if(((sto.deposit_freq==0) && (numMonth%12==0) && (numMonth>0)) ||
((sto.deposit_freq==1) && (numMonth%3==0) && (numMonth>0)) ||
((sto.deposit_freq==2) && (numMonth>0)))
monthlyDep = sto.deposit_regular
else
monthlyDep = 0
cumMonthlyRet += monthlyRet // do the cumulative monthly return
// interest earning is pre-taxed.
totalInterest = totalInterest + monthlyRet
// net saving is either taxed or not
if((sto.taxrate_istaxed == 1) && (numMonth%12 == 0) && (numMonth>0)) {
totalTax = totalTax + cumMonthlyRet * sto.taxrate_taxrate
prevBal = (prevBal + monthlyDep) -
cumMonthlyRet * sto.taxrate_taxrate
cumMonthlyRet = 0
}
else
prevBal = (prevBal + monthlyDep) + monthlyRet
if(prevBal >= goal) // stop looping if goal has been reached
break;
// move to next deposit
numMonth = numMonth + 1
if(numMonth > 1200)
break;
}
if(numMonth < 1200) {
sto.result_saved = Math.round(prevBal)
sto.result_interest = Math.round(totalInterest)
sto.result_tax = Math.round(totalTax)
sto.result_term = numMonth
sto.result_regular = sto.deposit_regular
}
else {
sto.result_saved = 0
sto.result_interest = 0
sto.result_tax = 0
sto.result_term = 0
sto.result_regular = 0
}
numYear = Math.round((sto.result_term/12) - (sto.result_term%12)/12)
numQuarter = Math.round((sto.result_term/3) - (sto.result_term%3)/3)
if(sto.deposit_freq == 0) // yearly deposit
sto.result_deposit = sto.deposit_regular * numYear
else if(sto.deposit_freq == 1) // quarterly deposit
sto.result_deposit = sto.deposit_regular * numQuarter
else if(sto.deposit_freq == 2) // monthly deposit
sto.result_deposit = sto.deposit_regular * numMonth
sto.result_deposit = Math.round(sto.result_deposit)
}
<%
// This function returns a annual deposit amount for a given intial
// balance, term, and goal. The calculation is a rather invovled one.
// Here is the formula that calculates it.
//
// X = (S_n - a_0 * b_0^n) / (b_0^(n-1) + b_0^(n-2) + ... + b_0)
//
// where a_0 is the initial balance, the n is the number of years to
// save, the b_0 is the (1 - tax_bracket) + (1 + interest/12)^12 * tax_bracket.
%>
// The X gives the annual deposit amount.
//
function getResultSet3()
{
var sto = parent.data
var monthlyInt = sto.interest_rate / 12
var b0, bn, i, regular
var totalInterest, yearlyRet, prevBal, totalTax
// calculate the b0 for monthly compounding
b0 = 1 + monthlyInt
b0 = Math.pow(b0, 12)
if(sto.taxrate_istaxed == 1) {
b0 = sto.taxrate_taxrate + b0 * (1 - sto.taxrate_taxrate)
}
// calculate the denominator
bn = b0
if(monthlyInt != 0) {
bn = (1 - Math.pow(b0, sto.deposit_term)) / (1 - b0)
if (bn==0)
bn = 1
}
else
bn = sto.deposit_term
// calculate annual deposit amount
regular = (sto.deposit_goal -
sto.deposit_balance * Math.pow(b0, sto.deposit_term)) / bn
// calculate total interest earned
prevBal = sto.deposit_balance
totalInterest = 0
totalTax = 0
for(i=0; i<sto.deposit_term; i++) {
yearlyRet = annualDeposit(prevBal, sto.interest_rate)
totalInterest = totalInterest + yearlyRet
if(sto.taxrate_istaxed == 1) {
totalTax = totalTax + yearlyRet * sto.taxrate_taxrate
prevBal = (prevBal + regular) +
yearlyRet * ( 1 - sto.taxrate_taxrate)
}
else
prevBal = (prevBal + regular) + yearlyRet
}
sto.deposit_freq = 0
if(regular < 0)
sto.result_regular = 0
else
sto.result_regular = Math.round(regular)
sto.result_saved = sto.deposit_goal
sto.result_term = sto.deposit_term * 12
sto.result_deposit = sto.result_regular * sto.deposit_term
sto.result_interest = Math.round(totalInterest)
sto.result_tax = Math.round(totalTax)
}