Create a Countdown by recursion

Tell us what’s happening:
i don’t quiet understand

const countArray = countup(n - 1);          =>  5, 4 , 3, 2, 1    ""minus 1 with every loop""
    countArray.push(n);             => the .push() will add the value of "n" at the end of array.

so the logical final result should [ 5, 4, 3, 2, 1] but the example shows the reverse

in the same way

const countArray = countup(n - 1);          =>  5, 4 , 3, 2, 1    ""minus 1 with every loop""
    countArray.unshift(n);             => the .unshift() will add the value of "n"  at the beginning of 
                                                                          the array

so the logical final result should [ 1,  2,  3, 4, 5] 

and also is this syntax correct    .push(n - 1)

Your code so far


// Only change code below this line
function countdown(n){
if (n < 1) {
  return [];
} else {
  const down = countdown(n - 1);
  down.unshift(n);
  return down;
}
}
// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

Yeah, push puts the value at the end of the array. But countup(n) always returns the values from 1 to n in an array from smallest to largest.

  const countArray = countup(n - 1);
  countArray.unshift(n);

So the results of these two lines of code will first make an array from 1 to n-1 and then push n onto the end of that array.

  const countArray = countup(n - 1); // [1, 2, 3, 4, ..., n - 1]
  countArray.unshift(n); // [1, 2, 3, 4, ..., n - 1, n]

well the exercise reversed the whole thing
.push => [1, 2, 3, 4, …, n] while it should be [n, …, 1]
then unshift reverse the work

so either i don’t understand or something went wrong .

I really want to fully understand this subject clearly.

It should not be [n, ... 1]. Push puts values at the end of the array.

It seems like you are perhaps not treating the function calls separately in your mind.


When JS makes a function call, the code stops until the function call is completed.

For example

function timesTwo(n) {
  return n * 2;
}

let n = 5;
let result = timesTwo(n); // This is 10
result += n;
console.log(result); // This will log 15

This code fully executes the timesTwo() function before proceeding.


The same thing happens in recursive function calls.

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 ]

The function call must always be completed before the evaluation continues.

In this case,
countup(5) calls countup(4)
countup(4) calls countup(3)
countup(3) calls countup(2)
countup(2) calls countup(1)
countup(1) calls countup(0)
countup(0) returns []
so, countup(1) returns [].push(1) or [1]
so, countup(2) returns [1].push(2) or [1, 2]
so, countup(3) returns [1, 2].push(3) or [1, 2, 3]
so, countup(4) returns [1, 2, 3].push(4) or [1, 2, 3, 4]
so, countup(5) returns [1, 2, 3, 4].push(5) or [1, 2, 3, 4, 5]