Resolve Promises Sequentially
Run promises one after another and collect the results in order — built with .then chaining, no async/await.
JavaScript · ES6 — Pratik Rai ·
The same result as the recursive flatten, with your own stack instead of the call stack.
Write flattenIterative(arr) returning every value from a deeply nested array in order.
The function must not call itself. This is the follow-up an interviewer asks once you have written the recursive version, and the point is the depth limit: recursion runs out of stack, an explicit stack runs out of memory.
Input:
JSfile.javascript1flattenIterative([1, [2, [3, [4]]], 5]);
Output:
[1, 2, 3, 4, 5]
Order is preserved, however deep the nesting goes.
Goal: Flatten completely using an explicit stack, with no recursive call.
Continue learning with these related challenges
Run promises one after another and collect the results in order — built with .then chaining, no async/await.
JavaScript · ES6 — Pratik Rai ·
Group an array into buckets by a key function — now a real language feature as Object.groupBy.
JavaScript · ES6 — Pratik Rai ·
Turn a flat array of nodes with parentId into a nested tree — the shape behind every file explorer and nested menu.
JavaScript · ES6 — Pratik Rai ·
Run promises one after another and collect the results in order — built with .then chaining, no async/await.
JavaScript · ES6
Pratik Rai ·
Group an array into buckets by a key function — now a real language feature as Object.groupBy.
JavaScript · ES6
Pratik Rai ·
Turn a flat array of nodes with parentId into a nested tree — the shape behind every file explorer and nested menu.
JavaScript · ES6
Pratik Rai ·