Basic JavaScript - Use Recursion to Create a Countdown

Tell us what’s happening:
Hello everyone. I know that here are alot of topics and alot of discussions running down because of recursion. I readed alot and tons of examples just confused me because I couldnt see the forest cause of the trees. Sometimes the simplest things are hardest to understand.

I understood that my function repeats hisself until the base case. After the base case my variable arr is now defined with an empty array which allows my function to unshift values into my existing empty array but how does my function know the values? where are the values stored that my function can go backwards to put the values into my array?

I am really sorry that I created a new topic but I thought its better to start over with my state of understanding and creating my own topic to fill the missing understanding.

Your code so far

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

console.log(countdown(5))

Your browser information:

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

Challenge: Basic JavaScript - Use Recursion to Create a Countdown

Link to the challenge:

1 Like

try console.log them out :grin:

console.log([], n) // before base case return
console.log(arr, n) // before return

image

1 Like

I do not know how I console.log them correctly out, to understand the step of the stored values which are getting .unshifted afterwards … I feel really stupid ( in a funny way) because I know exactly that it is so simple … directly in front of my eyes but yet I have to figure it out.

thank you really much. a little bit of trying and taking a walk got me a step forward in deeper understanding of recursion. Have a nice night <3

1 Like

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