class Mortgage { constructor(principal, years, rate) { this.principal = principal; this.years = years; this.rate = rate; } get monthlyPayment() { let monthlyRate = this.rate / 100 / 12; return this.principal * monthlyRate / (1 - (Math.pow(1/(1 + monthlyRate), this.years * 12))); } get amortization() { let monthlyPayment = this.monthlyPayment; let monthlyRate = this.rate / 100 / 12; let balance = this.principal; let amortization = []; for (let y=0; y { let principal = document.getElementById("principal").value; let years = document.getElementById("years").value; let rate = document.getElementById("rate").value; let mortgage = new Mortgage(principal, years, rate); document.getElementById("monthlyPayment").innerHTML = mortgage.monthlyPayment.toFixed(2); document.getElementById("monthlyRate").innerHTML = (rate / 12).toFixed(2); let html = ""; mortgage.amortization.forEach((year, index) => html += ` ${index + 1} ${Math.round(year.principalY)}
${Math.round(year.interestY)} ${Math.round(year.balance)} `); document.getElementById("amortization").innerHTML = html; });