Recursion question

Hi,
I hope someone can help me, I can’t figure out why countdown(n-1) is 1 instead of 4. I logged before and after the recursion case, my thinking is that when the function calls itself…it’s countdown(5-1) // 4

Thank you.

function countdown(n){
    if(n < 1){
     return [];
    } else {
    console.log(n); // first log is 5 
    let array = countdown(n-1); //
    console.log(n); //first log is 1
    array.unshift(n);
    return array;
    }
}
console.log(countdown(5)); // [5, 4, 3, 2, 1]

Welcome to freecodecamp forum @RomeoB! :smiley:
You could have opened a new thread for your problem to mantain the discussion linear, anyway: in your code, the execution of the function countdown stops at this line:

let array = countdown(n-1); //

Then it ‘restart’ the function with the argument n -1.
The first time the ‘function chain’ starts to return their values n is equal to 1, since you put as base condition n >= 1^^

Thanks for your reply, I understand what you mean when the function let array = countdown(n-1); makes sense (5-1) then (4-1)…

But the second part I still don’t understand, the first value that we .unshift into the array is 1 then 2 …etc.

A bit lost again also here when you say "The first time the ‘function chain’ starts to return their values n is equal to 1, since you put as base condition n >= 1 “” because I can only find in the code this "
n < 1 "

Thanks for your time.

1 Like

In the future, please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.

Thank you.

if you use only var to declare your variables you can use this tool: (it is a limitation of the tool)
http://www.pythontutor.com/javascript.html

otherwise I can help you on multiplayer mode on repl.it - so we can see live the same code we also instant chat messaging

Hi,

Thank you for your time, my username on repl.it is @RomeoB1984. Whenever you have time please contact me there and hopefully you can help me see things clearly.

I appreciate your time.

try looking at it here

2 Likes

But the second part I still don’t understand, the first value that we .unshift into the array is 1 then 2 …etc.

Exactly!
When you reach the base condition you return the empty array, []; then the function where you want to unshift the element ‘1’ returns, then the one with 2, …
That’s also what i meant when i wrote the statement below:

A bit lost again also here when you say "The first time the ‘function chain’ starts to return their values n is equal to 1, since you put as base condition n >= 1 “” because I can only find in the code this "
n < 1 "

Probably @ilenia 's code will clarify better the concept i hope^^
If you need more help just post for it, good luck! :slight_smile:

Yeah, we did look at it together, took it apart, I still have to go through it and also compare that code to other examples less complex to get the real understanding. Thanks again for your time.