home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 November
/
pcwk_11_98a.iso
/
Wtestowe
/
Money99
/
money99.1
/
mortgage
/
logic.js
< prev
next >
Wrap
Text File
|
1998-07-25
|
4KB
|
123 lines
function min(x, y) {
if(x <= y)
return x;
else
return y;
}
function max(x, y) {
if(x >= y)
return x;
else
return y;
}
// -------------------------------
// Calculate values for Mortgage 1
// -------------------------------
var rate1
var loan1PaymentsPerYear
var sto = parent;
if(sto.loan1Schedule == "annually")
loan1PaymentsPerYear = 1;
else
if(sto.loan1Schedule == "monthly")
loan1PaymentsPerYear = 12;
else // loan1Schedule == "biweekly"
loan1PaymentsPerYear = 26;
// Periodic Interest rate = 1 + (user-entered rate / 100 / periods per year)
rate1 = 1.0 + ( (sto.loan1Rate/100.0) / loan1PaymentsPerYear);
/*
To calculate periodic payment amount, first sum the periodic interest rate over the
total number of periods
set temp = 0
for n = 0 to (total number of periods - 1)
temp += (periodic interest rate to the power of n)
Then the periodic payment amount can be calculated:
Periodic payment amount = (balloon payment * (periodic interest rate - 1))
+ ((initial loan amount - balloon payment) * (the periodic interest rate to
the power of the total number of periods) / (interim value))
*/
for(var n=0, temptotal1 = 0; n < sto.loan1Length * loan1PaymentsPerYear; n++)
temptotal1 += Math.pow(rate1, n);
var payment1 = (sto.loan1Amount * Math.pow(rate1, (sto.loan1Length * loan1PaymentsPerYear))) /
temptotal1;
var interestTotal1 = 0;
var balance1 = sto.loan1Amount;
var iYears1 = min(sto.yearsUntilSale, sto.loan1Length);
for(n = 0; n < iYears1 * loan1PaymentsPerYear; n++) {
var tmpInterest1 = balance1 * (rate1 - 1.0);
interestTotal1 += tmpInterest1;
balance1 = balance1 + tmpInterest1 - payment1;
}
var pointTotal1 = Math.round(sto.loan1Fees + (sto.loan1Amount * ((sto.loan1Points / 100.0))));
var costTotal1 = Math.round(sto.loan1Amount + pointTotal1 + interestTotal1);
var interestTotal1 = Math.round(interestTotal1);
var paymentsTotal1 = iYears1 * loan1PaymentsPerYear;
payment1 = Math.round(payment1);
// -------------------------------
// Calculate values for Mortgage 2
// -------------------------------
var rate2
var loan2PaymentsPerYear
var sto = parent;
if(sto.loan2Schedule == "annually")
loan2PaymentsPerYear = 1;
else
if(sto.loan2Schedule == "monthly")
loan2PaymentsPerYear = 12;
else // loan2Schedule == "biweekly"
loan2PaymentsPerYear = 26;
// Periodic Interest rate = 1 + (user-entered rate / 100 / periods per year)
rate2 = 1.0 + ( (sto.loan2Rate/100.0) / loan2PaymentsPerYear);
for(var n=0, temptotal2 = 0; n < sto.loan2Length * loan2PaymentsPerYear; n++)
temptotal2 += Math.pow(rate2, n);
var payment2 = (sto.loan2Amount * Math.pow(rate2, (sto.loan2Length * loan2PaymentsPerYear))) /
temptotal2;
var interestTotal2 = 0;
var balance2 = sto.loan2Amount;
var iYears2 = min(sto.yearsUntilSale, sto.loan2Length);
for(n = 0; n < iYears2 * loan2PaymentsPerYear; n++) {
var tmpInterest2 = balance2 * (rate2 - 1.0);
interestTotal2 += tmpInterest2;
balance2 = balance2 + tmpInterest2 - payment2;
}
var pointTotal2 = Math.round(sto.loan2Fees + (sto.loan2Amount * ((sto.loan2Points / 100.0))));
var costTotal2 = Math.round(sto.loan2Amount + pointTotal2 + interestTotal2);
var interestTotal2 = Math.round(interestTotal2);
var paymentsTotal2 = iYears2 * loan2PaymentsPerYear;
payment2 = Math.round(payment2);
// Create summary statement detailing which mortgage will cost more
difference = costTotal1 - costTotal2;
if(costTotal1 == costTotal2)
strSummary = "After " + sto.yearsUntilSale + " years, <b>Mortgage 1 will cost you "
+ "the same</b> amount as Mortgage 2.";
else if(costTotal2 > costTotal1)
strSummary = "After " + sto.yearsUntilSale + " years, <b>Mortgage 1 will cost you "
+ intToDollarStr(Math.abs(difference)) + " less</b> than Mortgage 2.";
else // costTotal1 > costTotal2
strSummary = "After " + sto.yearsUntilSale + " years, <b>Mortgage 2 will cost you "
+ intToDollarStr(Math.abs(difference)) + " less</b> than Mortgage 1.";