Steamroller solution different from highlighted

Hello! I wanted to share a solution to Steamroller challenge, which I feel significantly differs from those, which are presented at https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-steamroller/16079. Unfortunately, I can’t post to the topic or modify it as wiki.

Here is the thing I intended to post. Review it, pls, if it has any value to that thread!

/* This solution is based on functional approach 
and doesn't use methods that weren't presented 
in previous challenges. */
function steamrollArray(arr) {
  // The idea is that `length` property is used 
to indicate that input `arr` is an array. As 
`String` prototype also has `length` property 
it is processed so that function could work with 
all primitives, including `string`s.
  if  (typeof arr == 'string') return arr;

  let result = [];

  // Read the head of the array and if it's 
primitive then put it in `result`, else 
recursively unfold the array.
  while (arr.length) {
    // Primitives doesn't have `length` property, 
so when function tries to access it `undefined` 
is returned.
    if (arr[0].length == undefined) {
      result.push(arr.shift());
    }
    // When _head_ is array-like it's processed 
recursively and concatenated with already 
processed elements.
    else {
      result = result.concat(steamrollArray(arr.shift()))
    }
  }
  return result;
}

I have blurred your code to avoid spoilers.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.