Basic JavaScript - Use Recursion to Create a Countdown

Tell us what’s happening:
The reason why this works seems very technical but I thought I got the gist of it. But I understood that the push(n) function only added n to the end of the array. Why then if I delete the .push part does it return a completely empty array instead of an array that counts up to 4?

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));

Your browser information:

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

Challenge: Basic JavaScript - Use Recursion to Create a Countdown

Link to the challenge:

because if you remove push you remove it for all calls to countup, meaning that the call to countup(n - 1) always results in an empty array

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