Skip to content

Instantly share code, notes, and snippets.

@lawwantsin
Created November 26, 2017 17:42
Show Gist options
  • Select an option

  • Save lawwantsin/9e654ea719a2780b66eafee721ab04c3 to your computer and use it in GitHub Desktop.

Select an option

Save lawwantsin/9e654ea719a2780b66eafee721ab04c3 to your computer and use it in GitHub Desktop.

Revisions

  1. lawwantsin created this gist Nov 26, 2017.
    21 changes: 21 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    var flatten = function(integers_array, flatten_array) {
    // If this function is called in recursion mode, then we
    // need to keep previous recursion results.
    var all_results = flatten_array || [];

    // We just want to perform any action if there's a
    // valid array input and this array contains any value in it.
    if (integers_array && integers_array.length > 0) {
    integers_array.forEach(function(value) {
    if (typeof value === 'number') {
    all_results.push(value);
    } else if (value instanceof Array) {
    flatten(value, all_results);
    }
    });
    }

    // At this point, all values were evaluated and we
    // have a flat Array of numbers.
    return all_results;
    };