Skip to content

Instantly share code, notes, and snippets.

@vampaynani
Last active July 22, 2023 01:47
Show Gist options
  • Save vampaynani/067125a09d16fb0405a2 to your computer and use it in GitHub Desktop.
Save vampaynani/067125a09d16fb0405a2 to your computer and use it in GitHub Desktop.
Fórmula CAT / Calculadora CAT
/*************
*
* @amount => Cantidad prestada neta(No se restan cargos ni se suman intereses)
* @charge => Cargos por apertura
* @byPeriod => Pago periódico
* @totalPayments => Total de pagos(Años, Meses, Quincenas, Días)
* @periodsByYear => Periodos por año de acuerdo a Banxico(Días: 360, Semanas: 52, Quincenas: 24, Meses: 12, Trimestres: 4)
*
*************/
window.CAT = (function(){
function getCATValue(total, charge, pay, payments, periods, cat){
var i = 0;
var value = charge / Math.pow((1+(cat/100)), 0/periods);
for(i = 1; i <= payments; i++){
value += pay / Math.pow((1+(cat/100)), i/periods);
}
return total - value;
}
var CAT = {
get: function(amount, charge, byPeriod, totalPayments, periodsByYear){
var cat = 50, cat_min = 0, cat_max = 0, temp_cat;
var counter = 0, tempVal;
do{
tempVal = getCATValue(amount, charge, byPeriod, totalPayments, periodsByYear, cat);
if(tempVal > 0){
temp_cat = cat;
cat = (cat+cat_min)/2;
cat_max = temp_cat;
}else if(cat_max == 0){
cat_min = cat;
cat = cat + 50;
}else{
temp_cat = cat;
cat = (cat+cat_max)/2;
cat_min=temp_cat;
}
counter++;
}while(counter <= 25);
return {num: cat, formatted: cat.toFixed(2)}
}
};
return CAT;
}());
@vampaynani
Copy link
Author

Una vez que se cargó el script en el body del html, sólo se debe mandar llamar el método get de CAT y pasarle los parámetros mencionados, regresa un objeto con las variables num(float) y formatted(string) para facilitar su uso.
var cat = CAT.get(amount, charge, byPeriod, totalPayments, periodsByYear);

var cat = CAT.get(100000, 0, 10000, 12, 12);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment