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

129
Vistas
Algorithm problem solving: How to simplify the following javascript code?

Function description

when const mergeTable=[2], Expected function return value

{ rowSpan: 2 }
{ rowSpan: 0 }

when const mergeTable=[2,3]; Expected function return value

{ rowSpan: 2 }
{ rowSpan: 0 }
{ rowSpan: 3 }
{ rowSpan: 0 }
{ rowSpan: 0 }

when const mergeTable=[2,3,3,2]; Expected function return value

{ rowSpan: 2 }
{ rowSpan: 0 }
{ rowSpan: 3 }
{ rowSpan: 0 }
{ rowSpan: 0 }
{ rowSpan: 3 }
{ rowSpan: 0 }
{ rowSpan: 0 }
{ rowSpan: 2 }
{ rowSpan: 0 }

functions that need to be optimized, The function has been implemented, the code is redundant, how to optimize


const mergeTable = [2, 3, 3, 2]; // test ok

// How to simplify this function
function renderRowSpan(index) {
  for (let i in mergeTable) {
    i = Number(i);
    if (i === 0) {
      if (index === 0) {
        return { rowSpan: mergeTable[0] };
      } else if (index < mergeTable[0]) {
        return { rowSpan: 0 };
      }
    }

    if (i === 1) {
      if (index === mergeTable[0]) {
        return { rowSpan: mergeTable[1] };
      }
      if (index < mergeTable[0] + mergeTable[1]) {
        return { rowSpan: 0 };
      }
    }

    if (i === 2) {
      if (index === mergeTable[0] + mergeTable[1]) {
        return { rowSpan: mergeTable[2] };
      }
      if (index < mergeTable[0] + mergeTable[1] + mergeTable[2]) {
        return { rowSpan: 0 };
      }
    }

    if (i === 3) {
      if (index === mergeTable[0] + mergeTable[1] + mergeTable[2]) {
        return { rowSpan: mergeTable[3] };
      }
      if (
        index < (mergeTable[0] + mergeTable[1] + mergeTable[2] + mergeTable[3])
      ) {
        return { rowSpan: 0 };
      }
    }
  }
}

// execute function
for (let index = 0; index < 10; index++) {
  console.log(renderRowSpan(index));
}


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

0

I think this should work and produce dynamic result as per number of elements in mergeTable.

function renderRowSpan(mergeTable){
    const result = [];
    for (const item of mergeTable) {
        let i = item;
        while(i > 0) {
            result.push({ rowSpan: i === item ? item : 0 });
            i--;
        }
    }
    return result;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

function renderRowSpan(mergeTable){ 
    return mergeTable.reduce(function(o, n){
       o.push({rowspan:n}); 
       for(let i=1; i<n; ++i) o.push({rowspan:0}); 
       return o;
    }, []);
}
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