Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!