Clarify this recursion example

Tell us what’s happening:
This is what I understand with the recursion example posted below:

  • If the argument passed to countup is greater than 1; call the function within itself with (n - 1).
  • When n == 0, return [ ]

I’m not sure how the integers, 1 - 5, are being appended to the array after the const countArray = countup(n - 1) expression. At that point, n == 0, and 0 is not in the array.

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown

countup returns an array, which is stored in countArray. the push method adds n at the end of the array, then the array is returned

Could some one help me!

I understand until here
When n==0, return []

I dont understand why in the moment that reach
countArray.push(n);
n==1 if there is not a n++

Sorry my ignorance

Thanks for helping me

the last function called was countup(0) and this return []

but countup(0) has been called inside the function countup(1)

there is no n++ because there is an n-1 in the function call

so countup(3) is calling countup(2) which is calling countup(1) which is calling countup(0),countup(0) returns [] and now countup(1) can do something with that value, which includes the push method you quoted where n is 1 (as n is the function parameter and 1 the function argument)

When n == 0 (the base case up top), an empty array is returned, not a 0. You never push when n equals 0. The tricky part is that n = (n - 1).

First the function iterates backwards through 5 to 1, calling countup(n) on each value, but not pushing or returning anything until (n - 1) == 0. This is at countup(1 - 1). countup() takes the resolved value of (n - 1) as the actual value of the parameter n. Thus countup(1 - 1) returns an empty array([]), because n == 0. We never call countup(0 - 1).

Next we pop back through through previous function calls, starting with countup(2 - 1), and push the result of 2 - 1 (which is 1) to the array for countArray == [1]. Remember: countup(1-1) has already been resolved, we do not call it a second time. Next:

countup(3 - 1) pushes 2 so countArray == [1, 2],
countup(4 - 1) pushes 3 so countArray == [1, 2, 3], and
countup(5 - 1) pushes 4 so countArray == [1, 2, 3, 4]. Lastly we pop to the original function call:
countArray(5) and push 5 onto the array, thus giving us our final array to return: countArray == [1, 2, 3, 4, 5].

1 Like