Finished
33// convenience to get you started writing code faster.
44//
55
6export const flatten = () => {
7 throw new Error('Remove this line and implement the function');
6export const flatten = (input) => {
7 // Edge Case: Handle null or undefined input
8 if (input === null || input === undefined) {
9 return [];
10 }
11
12 // Edge Case: Handle non-array input
13 if (!Array.isArray(input)) {
14 return [input];
15 }
16
17 const result = [];
18
19 for (const item of input) {
20 // Edge Case: Skip null-like values (null and undefined)
21 if (item === null || item === undefined) {
22 continue;
23 }
24
25 // Edge Case: Handle nested arrays recursively
26 if (Array.isArray(item)) {
27 const flattened = flatten(item);
28 result.push(...flattened);
29 } else {
30 result.push(item);
31 }
32 }
33
34 return result;
835};
36
37// Handled Edge Cases: null input, undefined input, non-array input, null values in array, undefined values in array, nested arrays of any depth
Test NameStatus
empty
Pass
no nesting
Pass
flattens a nested array
Pass
flattens array with just integers present
Pass
5 level nesting
Pass
6 level nesting
Pass
undefined values are omitted from the final result
Pass
null values are omitted from the final result
Pass
consecutive null values at the front of the array are omitted from the final result
Pass
consecutive null values in the middle of the array are omitted from the final result
Pass
6 level nested array with null values
Pass
all values in nested array are null
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.