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

252
Views
Steamroller javascript code how to happen

pleases describe me recursion function how to happen flatten array in the final result. Mainly please describe in the if section

function steamrollArray(arr) {
  let answer = [].concat(...arr);
  console.log(answer)
  if(answer.some(Array.isArray)){
    return steamrollArray(answer);
  }
  return answer
}

let result = steamrollArray([1, [2], [3, [[4]]]]);
// console.log(result)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Have added console.log to help with understanding how the recursion works. Introduced a variable idx to help track the level of recursion.

function steamrollArray(arr, idx=0) {
  console.log('bgn--> recursion # ', idx);
  console.log('arr: ', JSON.stringify(arr));
  let answer = [].concat(...arr);
  console.log(
    'computed answer: ', JSON.stringify(answer)
  );
  if(answer.some(Array.isArray)){
    // "answer" has an array, so recurse to next level
    console.log(
      'at least ONE elt in "answer" is an array\n',
      '--> making recursive call from recursion #: ',
      idx
    );
    return steamrollArray(answer, idx+1);
  };
  console.log(
    'no elt in "answer" is an array\n',
    'no more recursion from recursion #: ', idx,
    ' answer: ', answer.join(),
    ' has ZERO arrays ...'
  );
  return answer;
}

let result = steamrollArray([1, [2], [3, [[4]]]]);
// console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0 }

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!