| 3 | 3 | // convenience to get you started writing code faster. |
| 4 | 4 | // |
| 5 | 5 | |
| 6 | export const flatten = () => { | |
| 7 | throw new Error('Remove this line and implement the function'); | |
| 6 | export 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; | |
| 8 | 35 | }; |
| 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 |