Stavros Bannoura
Stavros Bannoura

Reputation: 65

Mortgage Amortization Payment Calculations

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.0575

the 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 years

the formula for total interest amount is monthly payment x term Months - loan amount

the formula for total loan is loan amount + total interest

the formula for Mortgage Loan Balance is total loan - monthly payment

Use 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 + "<br>");
    }
} 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

Upvotes: 2

Views: 2988

Answers (1)

Stavros Bannoura
Stavros Bannoura

Reputation: 65

I edited my code with the help of those 3 comments and my friend who is much better at me in javascript. The main things that i did were add a .toFixed(2) function to 2 decimal points to solve the decimal issue in JS. I completely reorganized the variables, i put the monthly payments in the for loop like the comment said, the main issue with that was that i did not create a var for the loop to use for the mortgage loan balance and i also multiplied the interest by 100 to equal to a percentage and thats pretty much it, it outputs exactly what i want.

var LoanAmount = parseFloat(prompt("Enter the loan amount:",));
var NumOfYears = parseInt(prompt("Enter the term years:",));
var MortgageLoanBalance;
var TotalLoan;
var TotalInterestAmount;
var MonthlyPayment;
var I;

I = 0.0575;
MonthlyPayment = (((I / 12) * LoanAmount)  / (1- (Math.pow (1+ (I / 12), 
(NumOfYears * -12)))));
TotalInterestAmount = MonthlyPayment * (NumOfYears * 12) - LoanAmount;
TotalLoan = LoanAmount + TotalInterestAmount;
MortgageLoanBalance = TotalLoan - MonthlyPayment;

document.write("Your mortgage term in years: " + NumOfYears + "<br>");
document.write("Your mortgage interest rate is: " + I * 100 + "% <br>");
document.write("Your mortgage loan amount: $" + LoanAmount.toFixed(2) + "<br>" );
document.write("Your total interest amount: $" + TotalInterestAmount.toFixed(2) + 
"<br>");
document.write("Your total mortgage amount: $" + TotalLoan.toFixed(2) + "<br>"); 
document.write("<br>");
document.write("Your Monthly payments begins now for your " + NumOfYears + " years
" + "<br>");
document.write("<br>");

var m = MortgageLoanBalance;
for (m = MortgageLoanBalance; m >= 0; m--) {
document.write("Your Monthly Payment is: $" + MonthlyPayment.toFixed(2) + "<br>");
document.write("Your Mortgage loan balance this month is: $" + m.toFixed(2) +   
"<br>");
document.write("<br>")
    m -= MonthlyPayment;

if (m <= 0) {
    document.write("This is the Ending Amortization Calculator.....");
    }
}

Upvotes: 1

Related Questions