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: