let it go

Finished
44//
55
66/**
7 * @param {any[]} arr
8 * @returns {any[]}
7 * Flattens a nested array of any depth, excluding null and undefined values.
8 * @param {any[]} arr - The nested array to flatten
9 * @returns {any[]} - A flattened array with no null or undefined values
910 */
1011export const flatten = (arr) => {
11 throw new Error('Remove this line and implement the function');
12};
12 // Edge Case: Handle when input is not an array
13 if (!Array.isArray(arr)) {
14 return [];
15 }
16
17 const result = [];
18
19 for (let i = 0; i < arr.length; i++) {
20 const element = arr[i];
21
22 // Edge Case: Skip null and undefined values
23 if (element === null || element === undefined) {
24 continue;
25 }
26
27 // Edge Case: If element is an array, recursively flatten it
28 if (Array.isArray(element)) {
29 const flattened = flatten(element);
30 result.push(...flattened);
31 } else {
32 // Edge Case: If element is a primitive value, add it to result
33 result.push(element);
34 }
35 }
36
37 return result;
38 // Handled Edge Cases: input is not an array, null values, undefined values, nested arrays of any depth, primitive values
39};
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.