| test1 | let array1 = [1, 2, 3, [4,5], 6, 7, 8, 9,[111,222,[231,2411,[222,333,[5555555,5555555,[12039123,123012312]]]]]];
function flat(arr, wArr = []) {
if (Array.isArray(arr)) {
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
flat(arr[i], wArr);
} else {
wArr.push(arr[i]);
}
}
} else {
wArr.push(arr);
}
return wArr;
}
let result1 = flat(array1);
console.log(result1);
| ready |
| test2 | let array = [1, 2, 3, [4,5], 6, 7, 8, 9,[111,222,[231,2411,[222,333,[5555555,5555555,[12039123,123012312]]]]]];
function flat(arr) {
let result = [];
let stack = [...arr];
while (stack.length > 0) {
let curr = stack.pop();
if (Array.isArray(curr)) {
stack.push(...curr);
} else {
result.push(curr);
}
}
return result.reverse();
}
let result = flat(array);
console.log(result);
| ready |
| test3 | let array = [1, 2, 3, [4,5], 6, 7, 8, 9,[111,222,[231,2411,[222,333,[5555555,5555555,[12039123,123012312]]]]]];
function flat(arr) {
let result = [];
let stack = [arr];
while (stack.length > 0) {
let curr = stack.pop();
if (Array.isArray(curr)) {
stack.push(...curr);
} else {
result.push(curr);
}
}
return result.reverse();
}
let result = flat(array);
console.log(result);
| ready |