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

245
Views
what does it mean to mention the original array after an else operator?

I am studying by looking at different answers for question in freecodecamp and I came across this one that I can't figure out the last part:

function steamrollArray(arr) {
    const flat = [].concat(...arr);
    return flat.some(Array.isArray) ? steamrollArray(flat) : flat;
  }
  
  console.log(steamrollArray([[3, [[4]]]]));

Why does it say "flat" after the "else :", like what does that mean? It just mention the original array, there is nothing going on like .push() or other methods.

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

0

it is referring to the original flat variable in this line:

const flat = [].concat(...arr);

which will be returned if this line is false:

flat.some(Array.isArray)

about 4 years ago · Juan Pablo Isaza Report

0

The key is the return preceding the expression: flat is not being modified because it is flat and is ready to be returned as the final value of the function.

I think the function can be reworded more clearly like this:

function steamrollArray(arr) {
    const flat = [].concat(...arr);
    if (flat.some(Array.isArray)) { // check if the array is not flat
        return steamrollArray(flat); // if it is not flat, process it more
    }
    return flat; // return a completely flat array
}
  
console.log(steamrollArray([[3, [[4]]]]));
about 4 years ago · Juan Pablo Isaza Report

0

The line with the return basically does:

  1. If at least one item inside the flat array is another Array, it will process steamrollArray (so this is a recursive function)
  2. If the some method returns false, it will return the flat variable as it is. The flat variable will be eventually a number, meaning .some will return false and exit one level of the recursiveness and so on.

Running the code makes it easier to understand, as well as the documentation on the .some() JS function.

function steamrollArray(arr) {
    const flat = [].concat(...arr);
    return flat.some(Array.isArray) ? steamrollArray(flat) : flat;
  }
  
 console.log(steamrollArray([[3, [[4]]]]));
 //output = [3,4]

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!