Please help me understand this code -

Tell us what’s happening:
Describe your issue in detail here.

I couldn’t understand how this code works.

  **Your code so far**

// Only change code below this line

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

// Only change code above this line
  **Your browser information:**

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

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

You have to understand some part since u wrote it. What parts specifically are confusing you?

I couldn’t understand the part inside the else statement. How does the push statement work here?

Push takes a value and adds it to an array.

So those three lines say

  1. create an array counting up to n-1

  2. push n onto the array

  3. return the updated array

I know how the push statement work. I need to know how it pushes the value inside this code.
The output of this code will be : [1, 2, 3, 4, 5]. I need to know how it pushes the value 1 first and not 5.

Why not put in some console.log statements and see what it is doing and when?

Oh. I thought you said you were confused about the push statement above.

Did you read the order of steps I posted above? Each line happens in order, so the countup to n-1 is made before pushing n onto the array.

It’s really remarkable that you got the code to work without knowing how it works.

This code is from “JavaScript Algorithms and Data Structures” certification course, Use Recursion to Create a Countdown.

Ahh, it’s the example. It was in the spot where the solution you are working on goes, which confused me.

Can you elaborate this code for me, and help me understand it. Otherwise, I can’t complete the challange.

You can still put that in the code window, add some console.log statements and trace through the code. That is an important skill for a developer.

2 Likes

The key really is that the code is executed in the order it is written. Where do you think adding some console.log statements might help you see what’s going on?

Explaining that code is not as easy as it sounds.
Take a while to learn about recursion.
Helpful video to get you started:

1 Like

this function executes recursively each time by checking against a condition (n-1) is satisfied. each time (n-1) is true, the value of n is returned. in this case. countup of 5 i.e countup(5), n =5. at first 5 < 1, hence to the array countArray, it pushes n (5), then reduces n by one, that’s (5-1 = 4), then does the same process for n = 4. The same process of pushing 4 to the array happens then n is reduced by 1 it becomes 3. Each time n is returned while being reduced by 1 until when n < 1.,- no array is returned . Each pushed element is always placed at index 0, hence the result becomes [1,2,3,4,5]

Put in a console.log statement.
Here you can see how the computer thinks of your code.

I learnt this the hard way.
:sweat_smile:

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