Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Pardon me, but that's a clumsy way to do it.

    And it doesn't really answer the question. You'd still have to add up the interest paid from payment number XXX to the end of the loan.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  2. #17
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Old Pedant View Post
    Pardon me, but that's a clumsy way to do it.
    What is clumsy?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  3. #18
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    I do not understand clumsy but it's our official method ! See this page

    NB : With the wrong banker's methods, the real rate is not 12% (to take your example) but Math.pow(1.01,12) which is 12,68% !
    Last edited by 007julien; 02-20-2013 at 12:24 PM.

  4. #19
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by 007julien View Post
    I do not understand clumsy but it's our official method ! See this page

    NB : With the wrong banker's methods, the real rate is not 12% (to take your example) but Math.pow(1.01,12) which is 12,68% !
    Well, that is the interest compounded monthly rather than yearly. Of course, compounding can be continuous, as well as yearly, monthly etc.

    Clumsy in French is gauche or maladroit. Perhaps not exactly the same meaning.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  5. #20
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    It's clumsy because to answer the original question what you have to do is
    (1) Run the loop from the first period to the period number in question and then throw away any results you get from that.
    (2) Continue the loop to the end, summing up the individual interest amounts.
    (3) Present the final sum.

    In other words, you perform a whole bunch of operations just to get the same sum you can get via two calculations using the code I showed. (I presented it in several lines of code, for clarity, but there are really only 2 lines of code--other than getting and validating the input and displaying the result--needed.)

    Oh, what the heck:
    Code:
          form.calc.onclick = function( )
          {
              var pv = Number(form.loan.value);
              var periods = Number(form.months.value);
              var annual = Number(form.apr.value);
              var rate = Number(form.remaining.value) / 12 / 100;
              if ( isNaN(pv) || isNaN(periods) || isNaN(rate) || isNaN(remain) )
              {
                  alert("All values must be numbers"); // alert obsolete, bad usage
                  return;
              }
              var pmt = pv / ( ( 1 - Math.pow( 1 + rate, - periods ) ) / rate );
              var rintTotal = pmt * remain - pmt * ( ( 1 - Math.pow( 1 + rate, - remain ) ) / rate );
              document.getElementById("rintTotal").innerHTML = rinttotal.toFixed(2);
          }
    See? Only two *active* lines of code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  6. #21
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Eehhh... Okay, maybe it isn't all that clumsy. And if you don't understand or don't know the need formulas then I suppose it's easier to grasp.

    Code:
          form.calc.onclick = function( )
          {
              var pv = Number(form.loan.value);
              var pmt = Number(form.payment.value); // NEW!
              var periods = Number(form.months.value);
              var annual = Number(form.apr.value);
              var rate = Number(form.remaining.value) / 12 / 100;
              if ( isNaN(pv) || isNaN(pmt) || isNaN(periods) || isNaN(rate) || isNaN(remain) )
              {
                  alert("All values must be numbers"); // alert obsolete, bad usage
                  return;
              }
              var inttotal = 0;
              for ( var p = 1; p <= periods; ++p )
              {
                  var ipaid = pv * rate;
                  if ( p > periods - remain ) { inttotal += ipaid; }
                  pv -= ( pmt - ipaid );
              }
              document.getElementById("rintTotal").innerHTML = rinttotal.toFixed(2);
          }
    But here the user must supply the payment amount. Presumably pre-calculated by the lending institution.

    If it's not supplied, then you have to add in my line to calculate the payment.

    And so then you need all the code in red IN ADDITION TO my payment calc.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •