Consulting about loan calculator function - javascript

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.

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);

Related

Issues with JavaScript Calculation

Trying to do a simple JavaScript calculation
var money = $("#money").val();
var percentRate = "{{ $money->percent }}"/100;
var incomeMoney = money * percentRate;
If the $amount->percent is a whole number it will give a correct figure but if I use a decimal point it won't.
Eg if I assign 23 to $money->percent it will give me the correct figure but if I assign decimal like 23.5 it will give a wrong figure.
I believe the system is rounding the percentRate to 0.23 instead of 0.235. How do I get around this, Please?
Full code -
...

Using data in HTML to display ChartJS Doughnut chart

I have a method where I am display products and when those products are filtered it shows X (amount that matches the filter) OF X (total number in that category)
I want a corresponding doughnut chart us ChartJS, I need it to use the data points which I currently have set in the html as...
<h4>
<strong>17</strong>
of 17
</h4>
I was thinking od possibly wrapping these 2 numbers in a div and giving it a dataset but I am unsure how I would correspond this to ChartJS.
You can see this my current progress here above the filters sidebar - http://bootsandlaces.net/products/footballs/
Can anyone help me ?
Well if you have to pull data from the DOM here is the possible solution (not the prettiest one, but it works... ). Give the corresponding HTML elements ids e.g. amount and total.
var total = document.getElementById("total").innerText;
var amount = document.getElementById("amount").innerText / total * 100;
var left = 100 - amount;
if ( amount && total ) {
data.datasets[0].data = [amount, left];
}
See this working fiddle
And do some styling...

How can I use an exponent in javascript

I am trying to build a simple loan calculator using jQuery for practice. I have most of the code down, but am having trouble inserting the exponent for the formula. I am aware of the Math.pow, but not sure if I am implementing it properly. Here is the loan formula:
P ( r / 12 ) / (1 - ( 1 + r / 12 ) ^-m )
P = principal
r = interestRate
-m = loan term in months
Here is my code:
var months = ("#loanTerm" * -1);
var calc = Math.pow(1 + (interestRate / 12), months);
Here is the HTML:
Loan Term(Months): <input class="userInput" id="loanTerm" type='number'>
Not sure if I am doing something wrong with the Math.pow, or if there is a way I am able to simply set the months to an integer, I'm relatively new to jQuery so any help would be much appreciated. Thanks in advance.
After the exchange of comments, I'm fairly sure the underlying question is how to access the value of a form input with jQuery.
In jQuery you can get the value of an <input> with
$("#loanTerm").val()
In vanilla JavaScript you can use
document.getElementById("loanTerm").value
You can then explicitly convert it to an integer and make it negative like so (in jQuery):
var months = parseInt($("#loanTerm").val(), 10) * (-1);
To test if months is NaN (the user didn't enter anything, or entered non-numeric data), you can use isNaN().
Finally, here is a testbed you can experiment with to confirm the equation is working as expected. I hope this helps.

In Jquery what is the best way to calculate a fee based on percentage of an input value

I have a script making that is going to be a worksheet for debt counselors. On the worksheet are input fields that collect different types of debts, like credit cards and mortgages, which are summed up into a variable called subTotal. I also have an input field that will be set as the variable percent. This input field is an adjustable number between 5 and 30 that is going to represent a percent.
I am trying to figure out the best way to take these variables and make a fee calculation. The result should be the percent of the subtotal. So for example if the var subTotal sums a total of 200 and somebody enters 15 into the input field that is represented by the variable percent . The calculation should be 30
Here are the variables
var subTotal = self.calculateTotalFor(elems);
// this the summed total for the debt input fields
var percent = $("#percent_field")
//I'm not sure if this is the best way to set this value because it needs to be a percent
Then I need to take those variables and get a fee calculation, but I know this method won't work because it does not add a decimal in front of the percent value.
total += (percent - 1) * subTotal;
Any ideas will be helpful as I am just a novice, thanks.
Try this:
var subTotal = self.calculateTotalFor(elems);
var percent = $("#percent_field").val(); // <------ The value....
var total = percent * subTotal / 100;

how to find out the vat from total amount and percetage value in javascript

This following code for get back to vat value from percentage value and amount value using java script but its not accuracy.
var vat=((25*100)/447);
vat=vat.toFixed(1);
OK, to help, you need to specify the details you are working with. What is the VAT rate? Are you working from the gross value (includes the VAT), or the nett value (excludes the VAT).
var nVatRate = 0.2;// This is the rate of VAT in the UK at present, 20%
function VatAmountFromGross(nGrossAmount){
return nGrossAmount / (1 + (1 / nVatRate));
}
function VatAmountFromNet(nNetAmount){
return nNetAmount * (1 + nVatRate);
}
So, change the VAT rate to match yours, which I am guessing is 25% (0.25).
Using "toFixed(1)" will ensure the value is fixed to 1 decimal place - usually you need two decimal places for VAT. You will also have rounding issues if you are summing values, and these cannot be helped.
Instead of this:
var vat=((25*100)/447);
vat=vat.toFixed(1);
You should be using exact total amount:
var vat=((24.585*100)/447);
vat=vat.toFixed(3);
What you should do while saving the values in the database is round of every value to three decimal, be it vat, percentage or total amount..and to present it to the user/client you can round it off to one or two decimal places.

Categories