Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda