Home › Forums › JavaScript › PMT Function
- This topic is empty.
-
AuthorPosts
-
September 28, 2014 at 8:37 pm #184921
web_editor
ParticipantHow to do the PMT function in pure/native Javascript? Like in pmt function of excel. How to do that? If I have different interest rate and number of payments. While the present values is the equal of amount – downpayment.
Pmt( interest_rate, number_payments, PV, FV, Type )
interest_rate : the interest rate for the loan.
number_payments : the number of payments for the loan.
PV : the present value or principal of the loan.
FV : It is the future value or the loan amount outstanding after all payments have been made.
Type is : It indicates when the payments are due. Type can be one of the following values:
0, 1Thanks
September 28, 2014 at 9:17 pm #184922__
ParticipantAre you asking how to do the math, or do you understand the math and are asking about how to write it in JS?
if you don’t understand the math
(or try google)if you do understand the math, try putting it into a function (e.g., make a pen) and then come back if you need help with a particular part. Make sure you also explain the math to us so we can help you effectively.
September 28, 2014 at 10:36 pm #184926web_editor
ParticipantI’m asking about the right JS of PMT
interest – already defined
fieldname22 – dropdown for no. of terms
fieldname1 – amount
fieldname2 – downpaymentSeptember 28, 2014 at 11:59 pm #184930__
ParticipantSo, is the “future value” or “payment type” important to you? or can they be left out?
Looking at your codepen, I have to ask how much experience you have with javascript at all. Do you understand the basic syntax? how to define variables? how to pass values to functions?
Let’s start with the “skeleton” of a function. Here’s a quick rewrite of what you had: instead of hardcoding variable names and relying on them existing in the global scope, we will deliberately pass the values in when the function is called.
I also preserved your formula (except for the last
0
, which caused a syntax error so I assumed it was a typo), using the new variable names. I don’t think the formula is correct, however. If you loaned me $10,000 at an 8.5% APR over 36 months, each payment would be approximately $5.2×10³⁶ (that’s 5.2 undecillion dollars. per month. for three years). Could you explain the formula?I’m signing off for now, but I’ll check back in tomorrow.
September 29, 2014 at 1:26 am #184931web_editor
ParticipantThe example that you give is correct. I will combine the PMT function into my equation
math.CEIL(PMT(amount-downpayment / (1- (1 / (1 + interest/12)*Math.EXP(num_of_terms))) interest/ 12));
September 29, 2014 at 11:13 am #184962__
Participantmm… okay
Let us know if you get stuck anywhere. Good luck!
BTW, what format is the interest rate expected to be provided in? a percentage? a decimal?
September 29, 2014 at 6:53 pm #185000web_editor
ParticipantThe format of interest is decimal. I’m stuck in PMT function in many hours
September 29, 2014 at 7:46 pm #185006__
ParticipantThe format of interest is decimal.
So, “8.5%” would be
.085
, correct? That’s what I had assumed, but it gave me the (obviously wrong) results above. Where did you find the formula you’re using? Are you sure it is correct?September 29, 2014 at 8:19 pm #185014web_editor
ParticipantI get the formula here
September 29, 2014 at 8:34 pm #185016__
ParticipantI get the formula here
Is there something about this that you need to change? It seems to work just fine as-is. The only thing I changed was to set the
fv
to0
if it was not passed.September 29, 2014 at 8:53 pm #185020web_editor
ParticipantWhat if the value of pv is the difference of amount-downpayment, while the ir is already define in my real equation
Here
function(){ var interest = 0; if(5000<=fieldname1 && fieldname1<=12000) { if(fieldname22 == 9) interest=0.994; else if(fieldname22 == 12) interest=0.9351; else interest=0; } if(12001<=fieldname1 && fieldname1<=30000) { if(fieldname22 == 9) interest=0.9383; else if(fieldname22 == 12) interest=0.917; else if(fieldname22 == 15) interest=0.8607; else interest=0; } if(30001<=fieldname1 && fieldname1<=60000) { if(fieldname22 == 9) interest=0.9383; else if(fieldname22 == 12) interest=0.8988; else if(fieldname22 == 15) interest=0.8429; else if(fieldname22 == 15) interest=0.7901; else interest=0; } return CEIL((fieldname1-fieldname2/(1-(1/(1+interest/12)*EXP(fieldname22)))/interest/12)/12); }
September 30, 2014 at 7:25 pm #185136__
ParticipantAlmost all of that should not be inside the function —at least, not this function. Functions should do only one thing, and do it well. Your
PMT
function should calculate payments and nothing else.Beyond this, functions need to be reusable. If you put all of that logic into your function, you’ll have to write a new PMT function every time you need to calculate a payment, because the previous PMT function only works in one specific situation.
If you have to do some calculation to get the
pv
, do it somewhere else (maybe in another function, maybe just in regular procedural code) and pass that value to thePMT
function. Same goes for your defined interest rate, etc.var totalAmount = 10000, downpayment = 1500, presentValue = (totalAmount - downpayment); var interestRate = .1499; var numberOfPayments = 36; var monthlyPayment = PMT( interestRate, numberOfPayments, presentValue );
September 30, 2014 at 7:39 pm #185137web_editor
ParticipantDo you mean I need two function right? One for PMT and second is for my real equation?
Is this right?
First function is for PMTfunction PMT ( ) { var totalAmount = fieldname1, downpayment = fieldname2, presentValue = (fieldname1- fieldname2); var interestRate = interest; var numberOfPayments = fieldname22; var monthlyPayment = PMT( interestRate, numberOfPayments, presentValue ) var fv=0; if( ! fv ){ fv = 0; } var pmt = ( interestRate * ( presentValue * Math.pow ( (interestRate +1), numberOfPayments ) + fv ) ) / ( ( interestRate + 1 ) * ( Math.pow ( (interestRate +1), numberOfPayments ) -1 ) ); return pmt; }
Second function (my equation)
Let’s say I have the second function
function(){ var interest = 0; if(5000<=fieldname1 && fieldname1<=12000) { if(fieldname22 == 9) interest=0.994; else if(fieldname22 == 12) interest=0.9351; else interest=0; } if(12001<=fieldname1 && fieldname1<=30000) { if(fieldname22 == 9) interest=0.9383; else if(fieldname22 == 12) interest=0.917; else if(fieldname22 == 15) interest=0.8607; else interest=0; } if(30001<=fieldname1 && fieldname1<=60000) { if(fieldname22 == 9) interest=0.9383; else if(fieldname22 == 12) interest=0.8988; else if(fieldname22 == 15) interest=0.8429; else if(fieldname22 == 15) interest=0.7901; else interest=0; } return CEIL(PMT(fieldname1-fieldname2/(1-(1/(1+interest/12)*EXP(fieldname22)))/interest/12)); }
September 30, 2014 at 8:40 pm #185139__
ParticipantYou’re getting there. However, your PMT function should only calculate payments. It should not do other things, like collecting values from inputs. The function you linked to should be usable as-is, with nothing added.*
* with the minor exception (which we discussed earlier) of giving
fv
a default value.Your second function might be called “getInterestRate” or something similar (it seems that’s what it does, correct?). As above, though, you shouldn’t be be collecting values inside the function. This should be done outside the function, and then the values should be passed to the function:
function getInterestRate( totalAmount, numberOfPayments ){ /* do your stuff */ } // and then pass the values in when you call your function: var interestRate = getInterestRate( fieldname1, fieldname22 );
October 6, 2014 at 7:38 pm #185619web_editor
Participant@__ I can’t get hmmm :| Can you give the whole code with the combination of my equation function and pmt function pls?
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.