5 kyu

flatten()

2,884 of 4,573jhoffner

Description:

For this exercise you will create a global flatten method. The method takes in any number of arguments and flattens them into a single array. If any of the arguments passed in are an array then the individual objects within the array will be flattened so that they exist at the same level as the other arguments. Any nested arrays, no matter how deep, should be flattened into the single array result.

The following are examples of how this function would be used and what the expected results would be:

flatten(1, [2, 3], 4, 5, [6, [7]]) // returns [1, 2, 3, 4, 5, 6, 7]
flatten('a', ['b', 2], 3, null, [[4], ['c']]) // returns ['a', 'b', 2, 3, null, 4, 'c']
flatten(1, [2, 3], 4, 5, [6, [7]]) # returns [1, 2, 3, 4, 5, 6, 7]
flatten('a', ['b', 2], 3, null, [[4], ['c']]) # returns ['a', 'b', 2, 3, null, 4, 'c']
flatten(1, [2, 3], 4, 5, [6, [7]]) # returns [1, 2, 3, 4, 5, 6, 7]
flatten('a', ['b', 2], 3, None, [[4], ['c']]) # returns ['a', 'b', 2, 3, None, 4, 'c']
Arrays
Algorithms

Stats:

CreatedMar 12, 2013
PublishedMar 16, 2013
Warriors Trained9663
Total Skips1483
Total Code Submissions36159
Total Times Completed4573
JavaScript Completions2884
CoffeeScript Completions131
Python Completions1628
Total Stars174
% of votes with a positive feedback rating90% of 578
Total "Very Satisfied" Votes487
Total "Somewhat Satisfied" Votes72
Total "Not Satisfied" Votes19
Ad
Contributors
  • jhoffner Avatar
  • docgunthrop Avatar
  • JohanWiltink Avatar
  • smartspot2 Avatar
Ad