Recursions countdown countup

Use recursion to Create a countdown challenge in JS (112/113):


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




  console.log(countdown(3+1)); 

  function countup(n) {
if (n < 1) {
  return [];
} else {
  const countArray = countup(n - 2); //Here I'm just trying out stuff... to see what happened...
  countArray.push(n);  // Here is my main concern, how is ( n)  pushed into countArray, if it has not been declared  [] after }else{ ? 
  return countArray;
}
}
console.log(countup(7));

Thanks in advanced to this wonderful community and to all those who bring aid for all the support, cheers!

  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

Hi @ebobea03 .

Recursion always executes the code of the basic case first. So, when “n” is less than -1, countArray will be initialized with [ ] (now countArray is an empty array).

After that the stack is getting unstacked. You know what that means? If you do, thats it.

1 Like

I appreciate your time and bringing me your perspective kind friend, I will need a little bit more of your patience, I will also need to study this deeply. But, how come these values are showing up in an order contrary to how they are being pushed or unshifted? The first value pushed is showing last in the array… many thanks again, sorry I’m frying my brains here… XD Your explanation has been of great help so far.

1 Like

@ebobea03 sorry for the lateness.

In recursion there is something call ‘stack’, or array if you prefer to call it that way. In this stack-array are being stored temporarily the process of each calling.

In other words, when you call your function you are executing a block of code right? Well, when you call it again, these mean that you don’t finished the process of that block code; you are storing the missing process of that code before calling the function again.

Answering your question of why the values are showing in order contrary. First, this depends of the method you are using, in these case ‘push’ method. Remember that the ‘push’ method always adds elements at the end of the array.

This would be the debugging:
n = 7 → countup(7-2)
n= 5 → countup(5-2)
n= 3 → countup(3-2)
n= 1 → countup(1-2)
n= -1 → [ ]

The debugging above is how normally works, BUT remember that these are being almacenated in a stack. In a STACK, the information almacenated are stored by an algorithm that works like these: first in, last out. So, the stack will stored the values like these:

  1. n= -1 → [ ]
  2. n= 1 → countup(1-2)
  3. n= 3 → countup(3-2)
  4. n= 5 → countup(5-2)
  5. n = 7 → countup(7-2)

That is why the case base is executed first and 7 is stored last.

FINALLY, when the case base is executed, then start the process of ‘unstacking’. So the second process that will be executed is number 2: 1. n= 1 → countup(1-2). Then we reach the part of countArray.push(n); in these case ‘n = 1’. SO 1 is the first value that is add to the array.

After finished executing process number 2, starts process number 3 and so on. Remember that the next values that will be stored at the end of the array.

1 Like

Dear sir, I appreciate very much that you took the time to help me, so this behavior has to do with the architecture of the physical computer, I has taken me some time to wrap this around my head, but your explanation has helped very much. Thank you mate.

1 Like

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