Didn't understand example

Tell us what’s happening:
I have a question about this example code given in challenge.
Your code so far

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

here:

else {
    const countArray = countup(n - 1);
    countArray.push(n);

“countArray” is not equal an array, how he is pushing “n” into countArray?

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Safari/537.36

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

countup always returns an array, so countArray is an array

Since it is a recursive function, it keeps calling itself before it can finish. The first time that function returns something is the base case where it returns an empty array. So, as ilenia says, it will never not return an array.

Yes, this is confusing. It is a hard concept. Look back through the forums for step-by-step explanations of what is happening.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.