#!/usr/bin/env node; /****************************************************************************** * This program... * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ const Utils = require('./Utils'); const main = async () => { if (process.argv.length != 5) { console.log(Utils.format("Syntax: {0:s} loan_amount rate_per_annum term_in_years", Utils.filename(process.argv[1]))); process.exit(1); } let balance = Utils.stoiWithDefault(process.argv[2], 0) * 100; let interestRate = Utils.stodWithDefault(process.argv[3], 0.05); interestRate /= 12.0; let periods = Utils.stoiWithDefault(process.argv[4], 30); periods *= 12; let payment = Math.trunc(interestRate * balance / (1.0 - Math.pow(1.0 + interestRate, -periods)) + 0.5); console.log("Month\tPayment\tPrinciple\tInterest\tBalance"); console.log(Utils.format("0\t\t\t\t{0:.2f}", balance / 100.0)); for (let i = 0; i < periods; ++i) { const INTEREST = Math.trunc(balance * interestRate + 0.5); let principle = payment - INTEREST; balance -= principle; if (i === periods - 1) { payment += balance; principle += balance; balance = 0; } console.log(Utils.format("{0:d}\t{1:.2f}\t{2:.2f}\t{3:.2f}\t{4:.2f}", i + 1, payment / 100.0, principle / 100.0, INTEREST / 100.0, balance / 100.0)); } } main().catch( e => { console.error(e) } );