I have to do an amortization schedule and the output should be: Mortgage term in years Mortgage interest rate Mortgage amount Total interest amount Total Mortgage amount
Monthly Mortgage Payments & Mortgage Loan Balance for each month. If the balance is 0 print "This is the Ending Amortization Calculator......"
Thanks!
<script>
var I = 0.0575;
var total_interest;
var total_loan;
var balance;
var P = parseFloat(prompt("Enter the loan amount $:", 0));
var Y = parseInt(prompt("Enter the term years ('30 or 15'):", 0));
var termMonths = Y * 12;
//if(Y != "30" || Y != "15"){
// alert("Please enter the term years: '30 or 15'");
// var Y = parseInt(prompt("Enter the term years ('30 or 15'):",0));
var month_payment = (((I / 12) * P) / (1 - (Math.pow(1 + (I / 12), (Y * -12))))).toFixed(2);
total_interest = parseFloat((month_payment * termMonths) - P).toFixed(2);
total_loan = parseFloat(total_interest + P).toFixed(2);
document.write("Mortgage term in years:", +Y + "<br>");
document.write("Mortgage Interest Rate: " + I + "<br>");
document.write("Mortgage amount: $" + P + "<br>");
document.write("Total Interest Amount: $" + total_interest + "<br>");
document.write("Total Mortgage Amount: $" + total_loan + "<br><br>");
var numPayments = 12 * Y;
for (var i = 0; i <= numPayments; i++) {
balance = parseFloat(total_loan - month_payment).toFixed(2);
document.write("Monthly Mortgage Payments: $" + month_payment + " & Mortgage Loan Balance for each month: $" + balance + "<br>");
}
if (balance == 0) {
document.write("This is the Ending Amortization Calculator......")
}
</script>
The issue is that you are not initializing balance. Also, a while loop would work better for this situation. What I've done here is set balance to total_loan before the loop. I end the loop before the balance - monthly_payment < 0, and at the end of the script I write that the balance is $0.00.