How can I make a recursive function return an array of a given number set to zero?

2 days ago 6
ARTICLE AD BOX

Given your implementation:

function rangeUp(n, numberList = []) { if (n < 0) return console.error("The number is invalid."); if (n == 0) return numberList.push(n); return rangeUp(--n, numberList); }

Ultimately, the result printed on the console was one regardless of the number entered.

The basic issue is that your recursive base case returns the result of numberList.push(n), which is the new length of the array after the value was pushed into it, i.e. length of 1 after 0 was pushed into the array.

See Array.push return value:

Return value

The new length property of the object upon which the method was called.

Then the regular recursive return case simply returns whatever out of the base case, again, only the value of the length of the array, 1.

Another issue is that you are mutatining the n variable as it is passed down through the recursive calls. Instead of --n you should use n - 1 as a "fresh" value of "n" for the next recursive call.

How can I make this code, according to the described exercise, work by returning the array of numbers or up to the entered value?

I want to create a recursive function that, given a number, returns the list of numbers from 0 to that number.

The recursive returns should build up the actual overall return value you want, i.e. if the base case is [0], then the next recursice call return should be [0, 1], and then [0, 1, 2] and so on until the all the recursive calls have been exhausted.

Another way to think about this is with the following representation:

n function representation result
n rangeUp(n) [...rangeUp(n - 1), n] [0, 1, .., n - 1, n]
n - 1 rangeUp(n - 1) [...rangeUp(n - 2), n - 1] [0, 1, .., n - 2, n - 1]
n - 2 rangeUp(n - 2) [...rangeUp(n - 3), n - 2] [0, 1, .., n - 3, n - 2]
..
2 rangeUp(2) [...rangeUp(1), 2 ] [0, 1, 2]
1 rangeUp(1) [...rangeUp(0), 1 ] [0, 1]
0 rangeUp(0) [0] [0]

Here you can see the recursion is on the first part of the array and you are appending the current depth-level n value to the array. You are effectively "substituting" the result of each row into the representation of the row above.

Here are a couple example implementations

This example looks more like the typical resursive solution wherein it implements a specific "base case" where it returns the initial result which will then be built up with the results of recursive calls.

function rangeUp(n) { if (n < 0) return console.error("The number is invalid."); // Base case, return initial array with zero pre-populated if (n == 0) return [0]; // Recursive case, return recursive result with current n appended return rangeUp(n - 1).concat(n); }
function rangeUp(n) { if (n < 0) return console.error("The number is invalid."); if (n == 0) return [0]; return rangeUp(n - 1).concat(n); } rangeUp(-1); console.log({ "rangeUp(5)": JSON.stringify(rangeUp(5)) }); console.log({ "rangeUp(1)": JSON.stringify(rangeUp(1)) }); console.log({ "rangeUp(0)": JSON.stringify(rangeUp(0)) });

This example uses the initially empty numberList array and applies all the recursion first, i.e. calling rangeup with the next lower n value until it reaches 0 and simply returns from all the recursive calls, appending the current n value to the numberList result array on the way back up.

function rangeUpRecursive(n, numberList = []) { if (n < 0) return console.error("The number is invalid."); // Recursive case, not at base 0 so recurse at n - 1 if (n > 0) { rangeUpRecursive(n - 1, numberList); } // For any `n` greater than or equal to `0` append to result array numberList.push(n); return numberList; } function rangeUp(n) { return rangeUpRecursive(n); }
function rangeUpRecursive(n, numberList = []) { if (n < 0) return console.error("The number is invalid."); if (n > 0) { rangeUpRecursive(n - 1, numberList); } numberList.push(n); return numberList; } function rangeUp(n) { return rangeUpRecursive(n); } rangeUp(-1); console.log({ "rangeUp(5)": JSON.stringify(rangeUp(5)) }); console.log({ "rangeUp(1)": JSON.stringify(rangeUp(1)) }); console.log({ "rangeUp(0)": JSON.stringify(rangeUp(0)) });
Read Entire Article