Iladarsda - 1 month ago 38

Javascript Question

I wrote small jQuery interest calulcator.

- it is calculating amount of credit from monthly re-payment

Have a look:

`//Get the value - monhtly repayment`

var InputedValue = $('form input[name="val1"]').val();

// 12 ,18, 24, 30, 36 - NumberOfMonths

for (var i = 12; i <= 36; i += 12) {

// j = 20 - Deposit in %

for (var j = 10; j <= 10; j += 10) {

// g = 20, 30 - InterestPerYear in %

for (var g = 10; g <= 10; g += 10) {

var InScaleOfYear = i / 12;

// Amount of payment excluding deposit

var AmountOfPayments = (InputedValue * (i - 1)); // rat bedzie o jedna mniej niz ilosc miesiecy, bo wplata poczatkowa

// Amount of payment including deposit

var AmountInTotal = (AmountOfPayments * 100) / (100 - j);

// Deposit

var Deposit = AmountInTotal - AmountOfPayments;

// Amount of payment in one year

var AmountOfPaymentInOneYear = (AmountOfPayments / InScaleOfYear);

var InterestPerYear = ((AmountOfPaymentInOneYear * g) / 100);

// Interest in total

var InterestInTotal = InterestPerYear * InScaleOfYear;

// Amount of credit

var AmountOfCredit = (AmountOfPayments + Deposit) - InterestInTotal;

$('table tbody').append('<tr><td>'

+ i + '</td><td>'

+ "at " + j + "% = " + Deposit.toFixed(2) + '</td><td>'

+ "at " + g + "% = " + InterestPerYear.toFixed(2) + '</td><td>'

+ "at " + g + "% = " + InterestInTotal.toFixed(2) + '</td><td>'

+ AmountOfPayments.toFixed(2) + '</td><td>'

+ AmountInTotal.toFixed(2) + '</td><td>'

+ AmountOfCredit.toFixed(2) + '</td></tr>');

}

}

}

Any of you working on something similar? How can I calculate interest having following information:

- number of month / years ()
`i = months`

- amount of monthly payment ()
`$('form input[name="val1"]').val();`

- amount of the deposit ()
`j = deposit`

- interest rate ()
`g = interest rate`

Answer

Your wording is a little confusing. But my understanding is that you want to figure out how much credit someone can afford based on a monthly payment and some interest rate. As others have pointed out, you first need to do this on paper to make sure you have the correct formula for the type of interest and compounding period. But that's not what StackOverflow is about, we're here to focus on programming! So let's assume credit card style lending with compound interest and monthly payments (you can rewrite for any other formula as needed).

The best resource I could find online for these formulas is here: Loan or Investment Formulas. This page has the compound formula pre-solved for almost any variable! So you don't need to remember that intro to business math class from college. Nice.

So now we just need to translate the correct formula into JavaScript. This is what our StackOverflow audience wants to see! In our case, where:

- A = Total Loan Amount
- P = Monthly Payment Amount
- N = Number of Months
- i = APR Interest Rate/12

A = (P/i)[1-(1+i)^-N]

- Ok so lets change out the square brackets for normal brackets. A = (P/i)(1-(1+i)^-N)
- Now we don't want JavaScript to think that first term is a method, we want it to multiply them. So lets add the asterisk. A = (P/i) * (1 - (1+i)^-N)
- JavaScript thinks a caret is a bitwise XOR operator, so for powers lets use Math.pow() instead. A = (P/i) * (1 - Math.pow((1+i),(-N))
Done! This can work in JavaScript. How about a nice human readable sample with comfortable variable names?

`var MonthlyPaymentAmount = parseFloat($('#txtMonthlyPayment').val()); var APR = 16.9 / 100; //16.9% APR var InterestRate = (APR / 12); //monthly interest for (var nMonths = 12; nMonths <= 36; nMonths += 12) { var TotalAmountOfPayments = nMonths * MonthlyPaymentAmount; var TotalAmountOfCredit = (MonthlyPaymentAmount / InterestRate) * (1 - Math.pow((1 + InterestRate), (-nMonths))); var TotalAmountOfInterest = TotalAmountOfPayments - TotalAmountOfCredit; alert("Total Loan Amount: $" + TotalAmountOfCredit + "Total Paid:" + TotalAmountOfPayments + ", Interest:" + TotalAmountOfInterest); }`

There you go! That'll calculate the total available credit based on some monthly payment, a given APR interest rate and 12, 24 & 36 month terms. If this wasn't exactly the goal you had in mind, go read that math tutorial and select another formula to translate to JavaScript!