-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay60.js
More file actions
29 lines (22 loc) · 935 Bytes
/
Day60.js
File metadata and controls
29 lines (22 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//* Write a function flattenArray that takes a nested
// array (an array that contains arrays) and returns a
// new array with all the elements from the nested arrays
// flattened into a single array.
// The function should be able to handle arrays nested to any depth.
function flattenArray(arr) {
let stack = [...arr];
let result = [];
while (stack.length) {
let next = stack.shift(); // Get the first element from the stack
if (Array.isArray(next)) {
stack.unshift(...next); // Push all elements of the nested array to the front of the stack
} else {
result.push(next);
}
}
return result;
}
// Example usage:
console.log(flattenArray([1, [2, [3, 4], 5], 6])); // Output: [1, 2, 3, 4, 5, 6]
console.log(flattenArray([[1, 2, [3]], 4])); // Output: [1, 2, 3, 4]
console.log(flattenArray([1, [2], [3, [4, [5]]]])); // Output: [1, 2, 3, 4, 5]