Using Recursions to Create Countdown, I don't understand it

Tell us what’s happening:
In the explanation n is decreasing but the final array values are increasing and I don’t understand why. Could anyone please explain it to me.
Thanks

Your code so far


// Only change code below this line
function countdown(n){
return;
}
// Only change code above this line

Your browser information:

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

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

I think this site might be able to help u since u can visualize it yourself
http://pythontutor.com/visualize.html#mode=edit
otherwise

So this is how recursion works

function russianDoll
{
//Base case If this falls then ->
if (we found the piece of chocolate)
return "yummy";
// -> it call's toward itself
else  if
russianDoll(smallerDoll)
//but what if there is no chocolate or a smaller doll
else (no more dolls)
return "No chocolate here "

We basicly have conditions that check over itself
In recursion we use a factorial number which is basicly like this
4!
4 * 3 * 2 * 1
24
Now if we want to write it out in code it would be

function factorialNumber(n)
{
if (n == 1 || n == 0)
return 1;  //just like the chocolate this is or base case
else return n * factorialNumber(n -1)
}
console.log( factorialNumber(4)) //this returns 24
now what happens is the next:

1st it checks over the base case but since thats not the case since 4 != 0|| 1
so it runs the next part of our code
4* factorialNumber(4-1 which sums up to 3)
then it does the same so we get

4* factorialNumber( 3)
3* factorialNumber( 2)
2 now 2 is the value of factorialNumber( 2) so it bubbles up
3 * 2 = 6 which is the value of 3 so it bubbles up again
same thing repeats and we are left with the value of 24


Did had help from YouTube so i don't own any credits for this

2 Likes

ok, first, this is the general diagram of the function countup, right?

Untitled Diagram

This instead is the whole flow of what happens:
(first follow the red arrows, then the blue arrows:

If you have any question please ask again, I will try to explain better

11 Likes

As this is very frequent question I’ve added a small guide for everyone to understand recursion better inside the actual guide for this challenge: freeCodeCamp Challenge Guide: Use Recursion to Create a Countdown

I’m pretty confident that diagrams @ilenia provided will make a lot of sense after reading the guide. :+1:

4 Likes

if you think it can be a good addiction to the guide… I spent too much time on that diagram

3 Likes

Totally! I’ve added the diagram and I think it’s a great addition, thanks!

Many thanks. This diagram actually helped me to understand this weird behaviour!

I found that the following solution allowed me to understand the concept a bit easier as it is a bit easier for me to step through what the function is doing (click to unblur).

Note I used countup here instead of countdown as it demonstrates the concept a little better. There is only one thing you need to switch to make it work for countdown, which you will probably work out straight away.

solution here

function countup(n) {
if (n < 1) {
return [];
} else {
return [...countup(n - 1), n];
}
}

If you run through it with an n of 5, you can follow what it is doing like so:

  1. create an array with 5 in last place
  2. what goes before the 5 in the array? Call the function again with 4 and spread the result of that function in the same array (with the ... notation)
  3. now 4 is in last place within its recursive step, but is in second last place overall because of the spread operator
  4. what goes before the 4 in the array? As before, call the function again with 3 and then spread the result of that function in the same array
  5. repeat until you reach your base case
  6. return your array

Hopefully that helps someone and doesn’t confuse things any further!

2 Likes

Hi @Development-Person !

Welcome to the forum!

This user has not been active since this post was made in May of 2020.
So I am going to close this thread.

I appreciate you wanting to help but I suggest participating in current topics.

Thanks!