JSFiddle - React, Tailwind, and code Playground
by borisjavier
JavaScript
// Suponiendo que numPagos y valorPago ya están definidos
const numPagos= 4;
const valorPago = parseInt(3174);
let acumulado = [];
let sumaAcumulada = 0;
// Generamos el arreglo acumulado original
for (let i = 0; i < numPagos; i++) {
sumaAcumulada += valorPago;
acumulado.push(sumaAcumulada.toFixed(4));
}
// Obtenemos el total de pagos restantes
const totalPagos = 2; //const totalPagos = window.pagRest();
// Calculamos el número de pagos que necesitamos en el arreglo final
const pagosRealizados = numPagos - totalPagos;
let acumReal;
if (totalPagos === numPagos) {
acumReal = Array(numPagos).fill("0.0000"); // Llenamos el arreglo con ceros en formato string
} else {
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]);
}
}
console.log(acumReal);