Код: Выделить всё
function computeLoan() {[b]//Prevent the Default Action eg, form to post/refresh the page
event.preventDefault();
var amount = parseInt(document.getElementById("amount").value);
var interest = calculateInterest(amount);
var term = parseInt(document.getElementById("years").value);
var frequency = document.getElementById("paymentTerm").value;
var finalAmmount = calculateMortgage(amount, interest, term, frequency);
document.getElementById("outMonthly").innerText = "$" + finalAmmount;
}
function calculateMortgage(p, r, n, f) {
r = percentToDecimal(r); //convert percentage to a decimal
n = yearsToMonths(n,f); //convert years to months
var pmt = (r * p) / (1 - (Math.pow((1 + r), (-n)))); //c=
((p*r)*Math.pow((1+r),n))/(Math.pow(1+r),n)-1
return parseFloat(pmt.toFixed(2));
}
function percentToDecimal(percent) { //Change the percent entered to
a decimal
return (percent / 12) / 100;
}
function yearsToMonths(year,frequency) {
//return year * 12;
if(frequency == "week"){
return year * 52;
}
if(frequency == "fortnight"){
return year * 26;
}
if(frequency == "quarter"){
return year * 4;
}
return year * 12;
}
function calculateInterest(amount){
var interest = 5.4;
if(amount > 200000 && amount < 250000){ //If loan amount is between $200,000 and $250,000, the interest rate will be 5.09%
interest = 5.09;
}
if(amount > 250000 && amount < 500000){ //If loan amount is between $250,000 and $500,000, the interest rate will be 4.84%
interest = 4.84;
}
if(amount > 500000 && amount < 750000){ //If loan amount is between $500,000 and $750,000, the interest rate will be 4.79%
interest = 4.79;
}
if (amount > 750000){ //If loan amount is greater than $750,000, the interest rate will be 4.50%
interest = 4.50;
}
return interest;
}
function postPayments(payment) {
var htmlEl = document.getElementById("outMonthly");
htmlEl.innerText = "$" + payment;
// document.getElementById("outMonthly").innerText = payment;
return;
}< /code>
form{
text-align: center;
border: 2px black solid;
}< /code>
Mortgage Calculator
Number of Years[/b]:
Loan Amount[/b]:
Payment Frequency :[/b][b]
Weekly
Fortnightly
Monthly
Quarterly
Yearly
The repayment amount is each [/b]
Подробнее здесь: https://stackoverflow.com/questions/455 ... alculation