5 kyu

flatten()

2,897 of 4,602jhoffner

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

More By Author:

Check out these other kata created by jhoffner

Stats:

CreatedMar 12, 2013
PublishedMar 16, 2013
Warriors Trained9723
Total Skips1498
Total Code Submissions36241
Total Times Completed4602
JavaScript Completions2897
CoffeeScript Completions132
Python Completions1643
Total Stars174
% of votes with a positive feedback rating90% of 582
Total "Very Satisfied" Votes490
Total "Somewhat Satisfied" Votes73
Total "Not Satisfied" Votes19
Ad
Contributors
  • jhoffner Avatar
  • docgunthrop Avatar
  • JohanWiltink Avatar
  • smartspot2 Avatar
Ad