relax

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 *
9 * @param {any[]} arr - The nested array to flatten
10 * @returns {any[]} - A flattened array with all null and undefined values removed
11 *
12 * @example
13 * flatten([1, [2, 6, null], [[null, [4]], 5]])
14 * // returns [1, 2, 6, 4, 5]
15 *
16 * @example
17 * flatten([null, [undefined, [1, 2]], 3])
18 * // returns [1, 2, 3]
919 */
1020export const flatten = (arr) => {
11 throw new Error('Remove this line and implement the function');
21 // Edge Case: Handle when input is not an array
22 if (!Array.isArray(arr)) {
23 return [];
24 }
25
26 const result = [];
27
28 for (const item of arr) {
29 // Edge Case: Skip null and undefined values
30 if (item === null || item === undefined) {
31 continue;
32 }
33
34 // Edge Case: If item is an array, recursively flatten it
35 if (Array.isArray(item)) {
36 const flattened = flatten(item);
37 result.push(...flattened);
38 } else {
39 // Edge Case: If item is a primitive value, add it to result
40 result.push(item);
41 }
42 }
43
44 // Handled Edge Cases: null/undefined values, non-array inputs, nested arrays of any depth, primitive values
45 return result;
1246};
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.