JSFiddle - React, Tailwind, and code Playground
by borisjavier
HTML
<input type="hidden" id="pR" value="[{"timestamp":"1731006298","txid":"66c7fb7acb4fc7ed58c33f41951edb06f6bad33220d48987099f4054723f0550"},{"timestamp":"1730964272","txid":"501a9448665a70e3efe50adafc0341c033e2f22913cc0fb6b76cbcb5c54e7836"},{"timestamp":"1730978672","txid":"501a9448665a70e3efe50adafc0341c033e2f22913cc0fb6b76cbcb5c54e7836"},{"timestamp":"1730993072","txid":"501a9448665a70e3efe50adafc0341c033e2f22913cc0fb6b76cbcb5c54e7836"},{"timestamp":"1731007472","txid":"501a9448665a70e3efe50adafc0341c033e2f22913cc0fb6b76cbcb5c54e7836"},{"timestamp":"1731021872","txid":"501a9448665a70e3efe50adafc0341c033e2f22913cc0fb6b76cbcb5c54e7836"}]">
<span class="text-success small pt-1 fw-bold" id="pagosTot"> </span> <span class="text-muted small pt-2 ps-1"></span>
<input type="hidden" id="tir" name="tir" value="11">
<input type="hidden" id="investi" name="investi" value="1">
<input type="hidden" name="perTot" id="perTot" value="0.083333">
<input type="hidden" id="unitPer" value="4">
JavaScript
(()=> {
const valorPresente = parseFloat(document.getElementById('investi').value);
console.log('valorPresente: ', valorPresente)
const tir = parseFloat(document.getElementById('tir').value) / 100;
const data = JSON.parse(document.getElementById('pR').value);
const targetTxid = "501a9448665a70e3efe50adafc0341c033e2f22913cc0fb6b76cbcb5c54e7836";
const count = data.reduce((acc, item) => {
return item.txid === targetTxid ? acc + 1 : acc;
}, 0);
const totalPagos = count;
console.log('totalPagos en calc:', totalPagos);
const perTot = parseInt(document.getElementById('perTot').value);
console.log('perTot: ', perTot)
const unitPer = parseInt(document.getElementById('unitPer').value);
const totalSeconds = 864e2;
const numPagos = Math.floor(totalSeconds / (unitPer * 36e2));
// Tasa de interés mensual
const tasaMensual = tir / 12;
// valor futuro
const valorFuturo = valorPresente * Math.pow((1 + tasaMensual), (12 * perTot));
// Calcular el valor de cada pago
const valorPago = valorFuturo / numPagos;
console.log('valorPago: ', valorPago)
let acumulado = [];
let sumaAcumulada = 0;
for (let i = 0; i < numPagos; i++) {
sumaAcumulada += valorPago;
acumulado.push(sumaAcumulada.toFixed(4));
}
const pagosRealizados = numPagos - totalPagos;
let acumReal = Array(numPagos).fill("0.0000");;
if (totalPagos !== numPagos) {
const pagosRealizados = numPagos - totalPagos;
acumReal = acumulado.slice(0, pagosRealizados);
// Rellenamos el arreglo hasta alcanzar numPagos con el último valor de acumReal
while (acumReal.length < numPagos) {
acumReal.push(acumReal[acumReal.length - 1]);
}
} else {
}
const result = {
vF: valorFuturo.toFixed(4),
pago: valorPago.toFixed(4),
acumulado,
acumReal
};
console.log(result)
}) ()