I am trying to solve a homework question that is asking me to calculate payments for a mortgage, but when i type in the mortgage amount say for example: 500000 and a term of 30 years, it does not update the monthly payments until 0. The javaScript application that will display an amortization schedule. The application will prompt the user to enter the loan amount (no comma). The application will prompt the user to enter the term years (30 or 15). The will calculate and display monthly payment at 5.75% interest.
Hints:
You can modify you last week mortgage application.
Set interest rate to
0.0575the formula to calculate monthly payment is
(((I / 12) * P) / (1-(Math.pow (1+ (I / 12), (Y * -12))))).toFixed(2);
I= interest rate
P= loan principal (loan amount)
Y= term yearsthe formula for total interest amount is
monthly payment x term Months - loan amountthe formula for total loan is
loan amount + total interestthe formula for Mortgage Loan Balance is
total loan - monthly paymentUse for loop to display payments and balance
Use if statement to print "Ending...."
Use
.toFixed(2)to convert a number to keep only two decimals
var I = 0.0575;
var LoanAmount = parseFloat(prompt("Enter the loan amount:",0));
var NumOfYears = parseInt(prompt("Enter the term years:",0));
var MortgageLoanBalance;
//var TermMonth = ;
var MonthlyPayment;
MonthlyPayment = (((I / 12) * LoanAmount) / (1- (Math.pow (1 + (I / 12), (NumOfYears * -12))))).toFixed(2);
var TotalInterestAmount = MonthlyPayment * (NumOfYears * 12) - LoanAmount;
var TotalLoan = LoanAmount + TotalInterestAmount;
MortgageLoanBalance = TotalLoan - MonthlyPayment;
//alert(MortgageLoanBalance);
if (MortgageLoanBalance > 0 ) {
document.write("Your " + NumOfYears + " year mortgage amount is " + MonthlyPayment + " per month." );
//else if (MortgageLoanBalance > 0)
document.write(TotalInterestAmount);
document.write(TotalLoan);
document.write(MortgageLoanBalance);
for (i = 0; i <= MonthlyPayment; i++) {
document.write("Month " + i + ": Your Monthly Payment " + MonthlyPayment + " and Mortgage loan balance " + MortgageLoanBalance + "
");
}
} else {
document.write("You have no payments");
//document.write("I don't know what you did.")
}
It is supposed to print out the 5 outputs:
Mortgage term in years
Mortgage interest rate
Mortgage amount
Total interest amount
Total Mortgage amount
I am not getting any error messages but it is not updating any of the monthly payments and just looks like a mess.
Output:
Your 15 year mortgage amount is 4.15 per month.247.0000000000001747.0000000000001742.8500000000001Month 0: Your Monthly Payment 4.15 and Mortgage loan balance 742.8500000000001
Month 1: Your Monthly Payment 4.15 and Mortgage loan balance 742.8500000000001
Month 2: Your Monthly Payment 4.15 and Mortgage loan balance 742.8500000000001
Month 3: Your Monthly Payment 4.15 and Mortgage loan balance 742.8500000000001
Month 4: Your Monthly Payment 4.15 and Mortgage loan balance 742.8500000000001