# Countdown using recursion

Tell us what’s happening:

so my code was based off what i perceived to be the opposite of the example recursive code which was count up. I was looking at the answer keys, all of which were different methods but none looked too similar to the format of the example count up function.

where was my train of thought lacking?

count up function below:

``````function countup(n) {
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
countArray.push(n);
return countArray;
}
}
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]
``````

``````
// Only change code below this line
function countdown(n){
if (n > 1) {
return [];
} else {
const countArray = countdown(n + 1);
countArray.push(n);
return countArray;
}

}
// Only change code above this line

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36`.

Challenge: Use Recursion to Create a Countdown

you want to make a countdown using a recursive method yes?

all you want is an array with the items reversed , yes ?
that is with the items shoved onto the opposite end , right ?

yes, that is exactly what I was trying to accomplish

``````// Only change code below this line
function countdown(n){
if (n > 1) {
return [];
} else {
const countArray = countdown(n + 1);
countArray.push(n);
return countArray;
}
}
``````

the first problem you have is in your base case.
for example say you want to countdown from 10… so then your argument will always be greater than 1… that condition will hit before you even get to your recursive part and you will just be returning an empty array

the second problem is the array method that you are using. you will not use push… you say you want to do the opposite so find the method that does the opposite. it makes sense?

thank you, i swapped the greater than to less than, changed + to - and replaced push with unshift, it worked.

so… the basecase of

``````if (n < 1) {

return [];
``````

is saying if n less than 1 return empty array? i feel like that of course is wrong… but how would you read that

You are correct. if N isn’t a positive integer than there is nothing to count down and the program returns an empty array.

hey my brother first of all good job for your work
if n less than 1 , it returns empty array
because we want our array countdown n from 5 to 1
so if n less 1 we don’t need the n.

I hope my explanation make sense .

1 Like

try looking at the countup function executing using this tool:

1 Like

for some people that pythontutor website could help, try it out… personally I don’t prefer it, maybe this explanation helps? its not correct, is just pseudocode:

``````    function countup(n) {
//without this go on forever
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
// what is countup(4)??
//I need to know what is countup(3)
//I need to know what is countup(2)
//I need to know what is countup(1)
//try countup(0), ive hit base case
//return [] (countArray has become defined as an empty array)

//countup(4, countup(3, countup(2, countup(1, []))))
//where the empty array above is in the last bracket would have been count(0) but we
//were stopped by the base case from continuing...these functions are piling/stacking //on top
//of each other. until i reach the base case that countarray is undefined.. it will become defined
//when the height of the pile is n

countArray.push(n);
//each call will continue where It left off, they are now resolving, so for each call
//i will:
//[].push(n)
//.push(n)
//[1,2].push(n)
//[1,2,3].push(n)
//the n above represents what the value of n was, and each function remembers its execution context, basically what the value of n was (plus some other stuff)

return countArray;
//the countarray is now [1,2,3,4]
}
}
console.log(countup(4));
``````

and also I would like to add that its slower because it has to remember all its context and it requires more memory because of that.

ok, but where can i visualize the .push(n) from the recursive function. Also, thank you very much for the very thorough response in psuedocode. I understand the concept that the basecase is what stops the recursion from going on forever, though I’m not sure how the array gets created within the existing function

which array are you referring to? the empty array or the completed array?

that empty array does not exist until youve reached the base case, which would happen at the top of the stack or when the stack is at height of N (whatever value that argument is that you passed), afterwards you start to loose these functions one by one as they are garbage collected after each one returns (i.e. they pop off the stack). when they are all finished your built up array is then returned. in the above figure note that the array has yet to be defined, in this case, ive passed 3 as the value for my argument

and this is why this recursion will take more memory because it needs to remember the previous, whereas with a classic loop it does not need to remember the contex of what these things were before.

where can i visualize the .push(n)

for that, I would advise you to use debug statements in your code.

try to reflect on the following:
each one of those functions is stuck in time on that line waiting for the one above it to return and only then it can advance forward, when you are conscious of this you will understand how the push is happening and the order in which it happens.

read this it helped me also:

I hope I’ve given a good explanation, perhaps a forum leader will comment if I’ve said something inaccurate or if something I’ve written could be worded better.

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