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.
Related
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);
I am trying to Math.floor a scientific notation, but at one point the number gets too big and my current method doesn't work anymore. This is what I am using atm
var nr = (number+"").length - 4;
if( nr > 1 ) {
nr = Math.pow( 10, nr );
number= Math.floor(number/nr)*nr;
number= number.toExponential(3);
}
When it becomes a scientific notation by default, I think that's e20+, than my .length method doesn't work anymore since the length it returns isn't accurate. I can think of a work around, and that's to find out the number after e, and update my nr to Math.floor it properly, but it seems like so much work to do something so simple. Here's an example number 8.420960987929105e+79 I want to turn this into 8.420e+79, is there a way I can Math.floor the third decimal point always, no matter what the number is? As it stands when I use toExponential(3) it always rounds the number. My numbers can get as high as e+200 easily, so I need an easier way of doing what I'm currently doing.
Edit: managed to find a work around that works besides Connor Peet's answer for anyone who wants extra options
var nr = 8.420960987929105e+79+"";
var nr1 = nr.substr(0,4);
var nr2 = nr.substr(4, nr.length);
var finalNr = Number(nr1 + 0 + nr2).toExponential(3);
This way is more of a hack, it adds a 0 after the 4th number so when toExponential rounds it up, it gets 'floored' pretty much.
I wrote a little snippet to round a number to a certain number of significant figures some time ago. You might find it useful
function sigFigs(num, figures) {
var delta = Math.pow(10, Math.ceil(Math.log(num) / Math.log(10)) - figures);
return Math.round(num / delta) * delta;
}
sigFigs(number, 3); // => 8.420e+79
So I've looked into this for several hours before finally giving up and asking help.
I'm currently trying to form fill a character sheet for Pathfinder (D&D 3.5 equivalent) using adobe acrobat. I want to make it so when I fill in my strength score it will auto fill out anything that has to do with strength.
More specifically I need it to take my ability score divide by two and subtract 5 for my ability modifier. But when I use 17 for instance as my Strength score my modifier is 4. I need it to round down not up.
I tried to subtract 5.5 instead and that works until its 10 or lower. At which point I have the opposite problem.
My current code is Strength/2-5
Use Math.floor() like this:
var score = 17.0;
result = Math.floor((score / 2) - 5);
alert(result)
Output:
3
Original:
Strength/2-5
(it worked but it needed to round down instead of up)
Final:
var a = this.getField("Strength")
event.value = Math.floor((a.value - 10) / 2)
Thank you for trying everybody! Process of elimination gets it done
Can someone please help me with the simplest question ever.
var rate = (month * "0.004545");
I simply need to get rate to be the above value + 1.
I just need (X * Y) + 1
All values are numbers.
Would truly appreciate this, I have already spent 1 hour.
Just add (+) and remove the "s:
var rate = (month * 0.004545) + 1;
See Arithmetic operators on MDN.
Also ensure that month is defined without "s.
EDIT: As it appears that you aren't being able to get this to work, here's a JSFiddle that demonstrates the code working for me.
var prerate = parseFloat(month) * 0.004545;
var rate = ++prerate;
I'm looking for a way to round up a number to the next closest multiple of 250. So for example if I had the following JS:
var containerHeight = $("#container").height();
...And we imagine the value of "containerHeight" was 680px, I would want a way to round up to 750px (if the value was 1007, it should round up to 1250). I suspect this requires a solution that is more complex than I anticipate. Or perhaps jQuery has a built in function that will make this feasible?
I suppose this is more of a math question than it is a jQuery question (but my jQuery syntax knowledge is also a bit limited :)
Any ideas / bits of help are greatly appreciated,
Thanks!
containerHeight = Math.ceil(containerHeight / 250.0) * 250;
function NearestMultiple(i, j) {
alert(Math.ceil(i/ j) * j);
}
NearestMultiple(1007, 250); //returns 1250
See example at http://jsfiddle.net/SUya9/1/
Or what James said too!
EDIT: I see you wanted to round up all the time...Updated fiddle, but James got her in 1.
simple
var rounded = Math.ceil(value / round) * round;
For those that are working with integers, and want a solution that avoids intermediate floating point, use this:
int roundedUp = alignment * ((value + alignment - 1) / alignment);
or without the division:
int mod = value % alignment;
if (mod > 0)
value += (alignment - mod);