Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

264
Vistas
Math.log instead of recursive method - flaw in my formula

I just got solved this problem: Conversion from VBA to JS - wrong output (thanks to Terry Lennox!)

But I'd rather would like to do the same calculation with Math.log in stead of the recursive method, my customer suggested.

I am having the following code:

var amount = parseFloat($('#input_loan_amount').val()); //value: 250000
var initial_fee = parseFloat($('#input_initial_fee').val()); //value: 3499
var monthly_fee = parseFloat($('#input_monthly_fee').val()); //value: 99
var monthly = parseFloat($('#input_monthly_interest').val()); //value: 0.41
var rate = parseFloat($('#input_installment').val()); //value:3499
$('#result_albert').html('Calculating ...');

if (amount > 0) {
  amount = amount + initial_fee;
  if (isNaN(monthly) == true) {
    monthly = 0;
  }
  if (isNaN(rate) == true) {
    rate = 0;
  }

  monthly = monthly / 100;
  var monthly2 = Math.round(monthly * 100) / 100;
  var months = -(Math.log(1 - (monthly * amount / (rate - monthly_fee)))) / Math.log(1 + monthly);

  months = -(Math.log(Math.round((1 - (monthly * amount / (rate - monthly_fee))) * 100) / 100) / Math.log(Math.round((1 + monthly) * 100) / 100));
}

The number before the dot is correct, but the decimals are not correct. I reckon, my formula months= is not quite correct, but my mathematic skills are not high enough ...

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

We can use the formula explained here: https://math.stackexchange.com/questions/3185889/how-do-i-solve-for-n-number-of-periods-in-a-loan-repayment-formula

enter image description here

To get us the number of months required or duration, this is basically what the NPER formula will give us in Excel.

// Using the formula n = log(A/(A-rP))
// A: monthly payment
// P: principal
// r: rate
function nper(A, P, r) { 
    return Math.log(A/(A-r*P))/Math.log1p(r);
} 

function calculate() {
    var amount = parseFloat($('#input_loan_amount').val()); //value: 250000
    var initial_fee = parseFloat($('#input_initial_fee').val()); //value: 3499
    var monthly_fee = parseFloat($('#input_monthly_fee').val()); //value: 99
    var monthly = parseFloat($('#input_monthly_interest').val()); //value: 0.41
    var rate = parseFloat($('#input_installment').val()); //value:3499
    
    amount=amount+initial_fee;
    monthly_payment = rate - monthly_fee;
    monthly=monthly/100;
                        
    // Using formula from https://math.stackexchange.com/questions/3185889/how-do-i-solve-for-n-number-of-periods-in-a-loan-repayment-formula
    const durationMonths = nper(monthly_payment, amount, monthly);

    const roundedDuration = Math.ceil(durationMonths);
    const runningAdm = roundedDuration * monthly_fee;

    $('#durationOutput').html(roundedDuration);
    $('#runningAdm').html(runningAdm);

}

calculate()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>


 <label for="input_loan_amount">Loan amount:</label>
 <input type="text" id="input_loan_amount" name="input_loan_amount" value="250000">
 <br>

 <label for="input_initial_fee">Initial fee:</label>
 <input type="text" id="input_initial_fee" name="input_initial_fee" value="3499">
 <br>
 
 <label for="input_monthly_fee">Monthly fee:</label>
 <input type="text" id="input_monthly_fee" name="input_monthly_fee" value="99">
 <br>
 
  <label for="input_monthly_interest">Monthly interest (%):</label>
 <input type="text" id="input_monthly_interest" name="input_monthly_interest" value="0.41">
 <br>
 
 <label for="input_installment">Fixed price:</label>
 <input type="text" id="input_installment" name="input_installment" value="3499">
 <br>
 
 <br>
 <bold>Duration (months): </bold><i id="durationOutput"></i><br>
 <bold>Running Adm.: </bold><i id="runningAdm"></i><br>
 <br><br>
 <button type="button" onclick="calculate()">Calculate</button>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda