The problem is that your recursive call does not pass the second argument.

Without passing it, each recursive call will just populate its own, new array. It does return that array to the caller, but the caller (making the recursive call) ignores that returned value, so all the work of the recursive call is for nothing.

So the easy fix is to change this:

} else {
    recursion(nE);

to this:

} else {
    recursion(nE, resultAry);
Answer from trincot on Stack Overflow
🌐
freeCodeCamp
freecodecamp.org › news › flatten-array-recursion
How to Flatten an Array in JavaScript Using Recursion
August 18, 2022 - If you carefully observe the above code, line 6 print1ToNo(currentValue + 1) is calling the same function with a new value (whatever the currentValue was, plus 1, i.e currentValue + 1). And it keeps doing it, until the currentValue goes past N, because that's when we told it to return. Now, this is what recursion means. Now, let's get back to our main problem – we need to flatten an Array.
Discussions

Referencing an Array inside a Recursive Function
So I understand how basic recursion works now. But I am having a hard time understanding why the second else statement’s ‘arr[n - 1]’ n is an index for the array. The way its written, I would expect to get the result o… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
November 30, 2021
Javascript recursion loop items to array - Stack Overflow
I am trying to make a small program that prompts a user to add items to a grocery list. I read about using recursion to loop. I understand a while loop would probably be better suited for this ta... More on stackoverflow.com
🌐 stackoverflow.com
Help with Recursive Arrays
Tell us what’s happening: I get how to make the code work. I understand what’s needed to complete it but I cannot visualize how it works in this instance. Where are the variables storing and how are they storing in the correct order? Is there a simulation that goes through each iteration ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
November 13, 2019
recursion - Array decomposing recursive function in JavaScript - Code Review Stack Exchange
What I need is to remove first word from the string again and again until only one word is left, and put it all into array. For example, I have this string as an input: Chicago IL 12345 United Sta... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
August 7, 2017
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Referencing an Array inside a Recursive Function - JavaScript - The freeCodeCamp Forum
November 30, 2021 - So I understand how basic recursion works now. But I am having a hard time understanding why the second else statement’s ‘arr[n - 1]’ n is an index for the array. The way its written, I would expect to get the result of [1, 2] [n-1] instead of arr[1]. From what we learned, dont we access arrays by referencing their relative variable’s name, so we would need to have made the arr a variable?
🌐
Medium
medium.com › @natelapinski › recursive-array-methods-better-javascript-through-haskell-62c47b02d08c
Recursive Array Methods. Better Javascript through Haskell. | by Nate Lapinski | Medium
December 30, 2019 - In this short article, we’re going to implement some of the most useful methods on Array.prototype using recursion. The goal is to give you some insight into how map, filter, and reduce work, and to demonstrate how to the think about array operations in Javascript in terms of recursion, similar to how Haskell and other pure languages operate on lists.
🌐
Selftaughttxg
selftaughttxg.com › 2023 › 01-23 › how-to-write-a-recursive-function-in-javascript-for-beginners
How to Write a Recursive Function in JavaScript for Beginners |
January 2, 2023 - So before we learn how to write a recursive function, let's understand each part needed to build it. The MDN web docs explain that the reduce() method is an iterative method. It runs a "reducer" callback function over all elements in the array, in ascending-index order, and accumulates them into a single value.
🌐
Quora
quora.com › How-do-I-add-items-to-an-array-in-JavaScript-recursively-and-return-that-array-1
How to add items to an array in JavaScript recursively and return that array - Quora
How would you loop through an array in JavaScript, add a word, then return an array without using map (JavaScript, arrays, loops, dictionary, and development)? How do you implement a function that sums all elements of an array using recursion in JavaScript?
Find elsewhere
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-recursion-function-exercise-4.php
JavaScript recursion function: Compute the sum of an array of integers - w3resource
if (my_array.length === 1) { return my_array[0]; } else { // Recursive case: pop the last element and add it to the sum of the remaining elements. return my_array.pop() + array_sum(my_array); } }; // Example usage: Calculate and print the sum of elements in the array [1, 2, 3, 4, 5, 6]. console.log(array_sum([1, 2, 3, 4, 5, 6])); ... See the Pen javascript-recursion-function-exercise-4 by w3resource (@w3resource) on CodePen.
🌐
DEV Community
dev.to › justin_m_morgan › loops-array-methods-and-recursion-45ng
Loops, Array Methods, and Recursion - DEV Community
November 19, 2021 - We'll start with the explanation of what a loop, as an abstraction, offers programmers. Then we'll discuss how "looping" manifests in Javascript. Finally we'll discuss how we can tackle the same problems with functional-programming strategies: array-methods/functions and recursion.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Help with Recursive Arrays - JavaScript - The freeCodeCamp Forum
November 13, 2019 - Tell us what’s happening: I get how to make the code work. I understand what’s needed to complete it but I cannot visualize how it works in this instance. Where are the variables storing and how are they storing in the correct order? Is there a simulation that goes through each iteration ...
🌐
GitHub
gist.github.com › davemackintosh › 4250004
JavaScript recursive array iterator · GitHub
December 10, 2012 - JavaScript recursive array iterator. GitHub Gist: instantly share code, notes, and snippets.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Recursion and stack
The code is short and easy to understand (hopefully?). That’s the power of recursion. It also works for any level of subdepartment nesting. ... We can easily see the principle: for an object {...} subcalls are made, while arrays [...] are the “leaves” of the recursion tree, they give immediate result.
🌐
Programiz
programiz.com › javascript › recursion
JavaScript Recursion (with Examples)
Here, we have a recursive function greet() without a base case. As you can see, greet() keeps calling itself until the program runs into an error (RangeError). ... Before we wrap up, let’s put your knowledge of JavaScript Recursion to the test!
🌐
EyeHunts
tutorial.eyehunts.com › home › flatten array javascript recursion | example code
Flatten array JavaScript recursion | Example code
March 30, 2023 - To flatten an array using recursion in JavaScript, you can create a function that checks each element of the array and, if it is an array, recursively calls itself on that subarray.
🌐
Medium
medium.com › @me.sonu300 › flatten-array-using-recursion-functions-in-javascript-e033e5114e21
Flatten array using recursion functions in JavaScript? | by Sonu Kumar | Medium
June 2, 2024 - //flatten below given array let arr = [1,44, [2, [3,9], 67], 9]; //using recurssion function recur(a) { let newArr = []; for (let i =0 ; i < a.length; i++) { const element = a[i]; if (Array.isArray(element)) { newArr.push(...recur(element)) } else { newArr.push(element) } } return newArr; } console.log(recur(arr)) output: [1,44,2,3,9, 67, 9] //we can also write the same code using foreach: function flattenArray(items) { const flat = []; items.forEach(item => { if (Array.isArray(item)) { flat.push(...flatten(item)); } else { flat.push(item); } }); return flat; } console.log(flattenArray(arr)) output: [1,44,2,3,9, 67, 9]
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript recursive function
JavaScript Recursive Function
November 15, 2024 - This tutorial shows you how to use the recursion technique to develop a JavaScript recursive function, which is a function that calls itself.