Can't resolve this: Use Recursion to Create a Countdown

Tell us what’s happening:
I am getting stuck with recursion
This is what i get so far
Recursion
is a stack like
1
1 + 1
1 + 1 + 1
by using push it adds the 1+ to it as it pushes it
This is in an array-ish like thingy

It works by calling itself

n is what is inputed is just a container
n = 5 would put 5 in n
its (n -1) because
[0, 1, 2 , 3 ,4 ,5]
1 2 3 4 5 6
1- 1 = 0

It works just like we learned in math class like when we have the sum of 1 + 1 1/2. the / comes first this is called precedence. It will not give out the out the inner function but, the result of it.

But, i don’t get how to do this in reverse ? How do i countdown?

Your code so far


// Only change code below this line
function countdown(n){
if (n < 1){ //  If the function is called with a number less than 1, the function should return an empty array. This is out base case.
return [];
}else{
  const countdown = countdown(n - 1);
  countdown.push(-n);
return countdown;
}

Your browser information:

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

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

what doy ou want to do here?

consider this, with push you add the numbers at the end of the array, right? so the 2 is added to the right of 1 and you get [1, 2] and so on

so, what can you do instead to put the 1 at the left of the 2 to get [2,1]?

1 Like

ieahleen thanks i feel so stupid now XD
I would use unshift since it would add a new item to the begin of the array
or use splice since it can both remove or add items to the array
Challenge is solved thank you <3