Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript PMT Function

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 25 total)
  • Author
    Posts
  • #184921
    web_editor
    Participant

    How 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, 1

    Thanks

    #184922
    __
    Participant

    Are 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.

    #184926
    web_editor
    Participant

    I’m asking about the right JS of PMT

    interest – already defined
    fieldname22 – dropdown for no. of terms
    fieldname1 – amount
    fieldname2 – downpayment

    here’s the codepen

    #184930
    __
    Participant

    So, 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.

    #184931
    web_editor
    Participant

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

    #184962
    __
    Participant

    mm… 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?

    #185000
    web_editor
    Participant

    The format of interest is decimal. I’m stuck in PMT function in many hours

    #185006
    __
    Participant

    The 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?

    #185014
    web_editor
    Participant

    I get the formula here

    #185016
    __
    Participant

    I 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 to 0 if it was not passed.

    #185020
    web_editor
    Participant

    What 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);
    }
    #185136
    __
    Participant

    Almost 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 the PMT 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 );
    
    #185137
    web_editor
    Participant

    Do you mean I need two function right? One for PMT and second is for my real equation?

    Is this right?
    First function is for PMT

    function 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));
    }
    #185139
    __
    Participant

    You’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 );
    
    #185619
    web_editor
    Participant

    @__ I can’t get hmmm :| Can you give the whole code with the combination of my equation function and pmt function pls?

Viewing 15 posts - 1 through 15 (of 25 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.