Why is count increasing

Tell us what’s happening:
i understand the basic principle here but i don’t quite get why it’s [1,2,3,4,5] (with n=5 and not [4,3,2,1,5]. seems 5-1=4 so that gets sent first, 4-1=3, 3-1=2, 2-1=1, 1-1=0 so push 5. what am i not getting?

Your code so far


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

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13421.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.199 Safari/537.36.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

Notice that const countArray = countup(n - 1); is before the push. At the time when push is executed countArray already contain all lower numbers, so n is added at the end. This applies also in the countup(n - 1); calls making numbers ordered appropriately.

yes i understand n being the last thing pushed into the array. but why are the first four digits in that order? why if n=5 is it 12345 not 43215?

Base case of this recursion is n < 1, function keeps calling itself until that base case is reached. Only then it’s possible to start unraveling recursive calls.

So when n < 1, empty array is returned.
Then call having n = 1 can push to array and return [1].
Then call having n = 2 can push to array and return [1, 2].
And so on, until all recursive calls finish, in this case ending up with [1, 2, 3, 4, 5]

I’m sure you’re right but I’m not getting it.
Why is n = 1 performed before n = 2? Seems it should be n-1(4), then n-1-1(3), and so on until the base case is reached (n-1-1-1-1-1=0)
which would push 4 into the empty array, then 3, 2, 1 , then n=5. so the array would look like [4,3,2,1,5]? I’m missing something.

Because you are using push instead of append
so your first return is [5] , second is [4,5] and so on

Exactly…using push seems like it would push 4 into the empty array first, then 3, 2, 1 to get [4,3,2,1] and finally 5? I’m confused be the language of the explanation I guess.
At first, this seems counterintuitive since the value of n decreases , but the values in the final array are increasing . This happens because the push happens last, after the recursive call has returned. At the point where n is pushed into the array, countup(n - 1) has already been evaluated and returned [1, 2, ..., n - 1]

Sorry you are wrong push is happening before recursive call so your first pushed value is 5 in array. Push actually insert new value in front of array and append insert it in last

n = 2 is called first, but it depends on the result it gets from n = 1, so without it it can’t finish and push 2 to the array. Until the base case is reached other calls can’t continue, because they depend on the result calculated by the recursive call.

// for n = 5
countArray = countup(5 - 1)
// for n = 4 
countArray = countup(4 - 1)
// for n = 3
countArray = countup(3 - 1)
// for n = 2
countArray = countup(2 - 1)
// for n = 1
countArray = countup(0)

// countup(0) is the first that can return result directly - empty array, so now it all can go in the reverse order:

// for n = 1
countArray = []
countArray.push(1)
// for n = 2
countArray = [1]
countArray.push(2)
// for n = 3
countArray = [1, 2]
countArray.push(3)
// for n = 4
countArray = [1, 2, 3]
countArray.push(4)
// for n = 5
countArray = [1, 2, 3, 4]
countArray.push(5)
1 Like

I guess I’m wondering why, once the empty array is reached, they all go in reverse order.

That’s simply following the order they were calling one another. countup(0) was called by countup(1), countup(1) was called by countup(2), etc.

Here’s nice tool that can somewhat help with visualization how this happens internally:
http://pythontutor.com/javascript.html#mode=edit

I know sometimes it’s very difficult to understand program’s execution in the beginning as I am also a beginner and face this same issue quite frequently. I have broken down the given block of code for you to understand. Try to see how it works at each step by commenting parts below that point. Go through this and let me know if it helped. Also ignore the “undefined” as it’s because the function isn’t returning anything.


function countup(n) {
if (n < 1) {
  return [];
} else {
  //const countArray = countup(n - 1);
/*okay so, I will break each step for you 
to understand. Let's see how this program 
executes itself at each step. Program keeps
calling itself till the base case is reached 
where n = 1. At that point we have n = 1, 
n = 2, n = 3, n = 4 and n =5 in queue in that order.*/ 

 //Now first of all n = 1 
 let countArray=[];
  countArray.push(1);
  console.log(countArray);
  
  
  //Then n = 2 gets pushed
  countArray.push(2);
  console.log(countArray);
  
  
  //Then n = 3 gets pushed
  countArray.push(3);
  console.log(countArray);
  
  
  //Then n = 4 gets pushed;
  countArray.push(4);
  console.log(countArray);


  //Final element in queue i.e. n = 5 gets pushed at this moment after waiting so long. 

  countArray.push(5);
  console.log(countArray);

}
}
console.log(countup(5))

Why is it in that order to begin with? Seems it should be in the opposite order since n=5 is the first one.

We call countup(5)
countup(5) pauses and calls countup(4)
countup(4) pauses and calls countup(3)
countup(3) pauses and calls countup(2)
countup(2) pauses and calls countup(1)
countup(1) pauses and calls countup(0)
countup(0) returns []
countup(1) resumes, pushes 1, and returns [1]
countup(2) resumes, pushes 2, and returns [1, 2]
countup(3) resumes, pushes 3, and returns [1, 2, 3]
countup(4) resumes, pushes 4, and returns [1, 2, 3, 4]
countup(5) resumes, pushes 5, and returns [1, 2, 3, 4, 5]
We receive [1, 2, 3, 4, 5]

1 Like

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