Mortgage CalculatorMortgage Calculator
function createAmortizationGraph(principal, downPayment, rate, term) {
// Will be implemented with SVG
} function createBalanceGraph(principal, downPayment, rate, term) {
const data = [];
for (let i = 0; i < term * 12; i++) {
let balance = principal - downPayment;
if (i % 12 === 11) {
balance -= monthlyPayment;
}
data.push({ period: Math.floor(i / 12), remaining: balance });
} return createLineGraph(data, 'Principal vs Time');
} function createTotalGraph(principal, downPayment, rate, term) {
const data = [];
for (let i = 0; i < term * 12; i++) {
let totalPaid = monthlyPayment * ((Math.pow(1 + rate, i + 1)) / rate);
if (i % 12 === 11) {
totalPaid += downPayment;
}
data.push({ period: Math.floor(i / 12), total: totalPaid });
} return createLineGraph(data, 'Total Amount Paid');
} function createLineGraph(data, title) {
const graph = document.createElement('svg');
graph.setAttribute('width', '800px');
graph.setAttribute('height', '300px');
graph.setAttribute('title', title); let currentX = 0;
let currentY = 20; for (const item of data) {
const xPosition = currentX + (item.period * (graph.getAttribute('width') / term));
const yPosition = currentY - (item.remaining * (graph.getAttribute('height') / principal)); graph.setAttribute(`rect${Math.floor(xPosition)}'`, 'stroke', 'currentColor');
} return graph;
}