Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Recursion to problem-solving. The ability to divide and coquer problems
- //until they're small enough to be managed directly.
- const flatten = ([first, ...rest]) => {
- if (first === undefined) {
- return [];
- }
- else if (!Array.isArray(first)) {
- return [first, ...flatten(rest)];
- }
- else {
- return [...flatten(first), ...flatten(rest)];
- }
- };
- console.log(flatten([[1,2,[3]],4]));
- console.log(flatten([1, 3, [4, 5, [9, 3], [14, 13, [9, [2]]]]]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement