kale

Finished
88 * @returns {any[]}
99 */
1010export const flatten = (arr) => {
11 throw new Error('Remove this line and implement the function');
11 // Edge Case: Input is null or undefined
12 if (arr === null || arr === undefined) {
13 return [];
14 }
15
16 // Edge Case: Input is not an array
17 if (!Array.isArray(arr)) {
18 return [];
19 }
20
21 const result = [];
22
23 for (let i = 0; i < arr.length; i++) {
24 const element = arr[i];
25
26 // Edge Case: Element is null or undefined - skip these values
27 if (element === null || element === undefined) {
28 continue;
29 }
30
31 // Edge Case: Element is an array - recursively flatten it
32 if (Array.isArray(element)) {
33 const flattened = flatten(element);
34 result.push(...flattened);
35 } else {
36 // Edge Case: Element is a non-array value - add it to result
37 result.push(element);
38 }
39 }
40
41 return result;
1242};
43
44// Handled Edge Cases: null/undefined input, non-array input, null/undefined elements, nested arrays, mixed data types
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.