Reputation: 25
I'm trying to do a loan calculator where you can input the amount you want to borrow, the amount of months that you plan to pay everything and select a type of credit (car, studies, home, etc) that determines the interest rate of each.
In JavaScript, I called values from Loan Amount - Months to Pay - dropdown list with Type of Credits that provides different interest values. I try to work around this formula and write it in text like this:
p*(r*(1+r)^n/1-(1+r)^n);
Am I right with the formula I'm using to get Fixed monthly payment -- am I right writing the formula in text/code way? I'm also doing the output this way:
document.getElementById("id-name").innerHTML = p*(r*(1+r)^n/1-(1+r)^n);
Is this the right way to do it or should i do the formula in another var z
and call innerHTML = z
?? Finally, this would be the full JS function with the input variables:
function CALCULADORA() {
var x = document.getElementById("list").value;
var i = x/100;
var c = document.getElementById("cuotas").value;
var y = document.getElementById("valor").value;
document.getElementById("CALCULATOR").innerHTML = y*(x*(1+x)^c/1-(1+x)^c);
}
/*
x is Interest rate in percentage given by a dropdown list
i is Percentage / 100
c is Months to pay loan
y is Loan value
*/
The issue is that not getting the full formula, my result is only the Loan value divided by months to be paid -- that is what I got displayed. Thanks to any help you can give me! This is one of my first codes.
Upvotes: 2
Views: 218
Reputation: 2733
Make sure you are converting those values to numbers before trying to math on them. Specifically when adding. Something like 1+x
will not give you what you expect when x is a string like 0.2
.
Assuming those values can be decimals, you will need to do something like:
var x = parseFloat(document.getElementById("list").value);
var i = x/100;
var c = parseFloat(document.getElementById("cuotas").value);
var y = parseFloat(document.getElementById("valor").value);
Upvotes: 0