Basic JavaScript - Replace Loops using Recursion

It’d push 1 into the array, then n -1 would be 0 so it would return []. I mean that’s what I’m expecting, obviously it’s not correct as it’s showing an error.

But what array? How did you make ‘the array’?


There is no ‘go back to the top of the function’.

The fundamental idea of recursion is to make a function call to solve a smaller version of the problem and then build the answer out of the return value of the recursive call.

I see what you are saying, but then the array isn’t made until return [];, ie: the last step. So whether n is 1, 5 or whatever, if the array isn’t made until that point I just can’t see what’s happening before ‘return

You did not answer the question.

You said

What array are you talking about?

Well it’d just make a new array and put 1 in it. I don’t know :laughing:

Ok this is the best I can explain my thinking/confusion. Aside from the general confusion, it’s the italics that are the main points of confusion:

  1. N is 5 and 5 is not less than 1 so 5 is ‘held’ somewhere. Let’s say 5 is put in the ‘to do box’
    2. N now becomes 4 which is called to the countup function again. 4 is added to the to do box
    3. Then 3 in the the to do box
    4. Then 2 in the to do box
    5. Then 1 in the to do box
    6. At this point 5,4,3,2,1 are dealt with the .push method. So the array should be [5, 4, 3, 2, 1]. Why it’s counting up and not down as the exercise says, I don’t know!
    7. Then 0 is < 1 so return a blank array. Not sure why it has to be a blank array, why not just spit out the array in the previous step?

Stop worrying about 5.

Think about the almost simplist case of 1

What do these two lines say? Only think about these two lines

It’s saying take 1, check is it’s less than 1, it calls the function again and checks if it’s less than 1 over and over so I guess there’s an infinite loop situation. But presumably 1 is also pushed to the array with .push(n) is it?

No. That is not what those two lines alone say.

But you got the important part here

This infinite series of function calls prevents

You never run the second line because you are stuck always running the first again and again and again.

1 Like

Go on then, what does it say?

I just said what they say (quoting works funky on my phone so I have to edit posts to get them to show up where I want)

1 Like

Right… So let me try with 1 as n but with the original code:

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

And now… at the point where I am pretty much loosing the plot with this, the only way I can figure how to convey my thoughts are to directly transcribe my internal thought process in 2 brain farts:

Brain fart 1

  1. So 1 is checked to see if it’s less than 1. It isn’t so over to ‘else’
  2. 1 is now held somewhere apparently? So 1 is chilling in limbo waiting for his calling in life.
  3. Meanwhile (or is it before/after something!?) const countArray = countup(n - 1); is dealing with 0. Well 0 sure as heck is less than 1 so return [] unceremoniously stops us in our tracks and gives us a fat blank array.
  4. 1 sits in limbo for the rest of eternity feeling redundant and generally unloved. Poor 1.

Brain fart 2

  1. 1 is checked to see if it’s less than 1. It isn’t so over to ‘else’
  2. 1 is not held in limbo here, 1 is joyously .pushed to the countArray variable. Good for 1.
  3. Meanwhile, const countArray = countup(n - 1); is dealing with 0. Well 0 sure as heck is less than 1 so return [] unceremoniously stops us in our tracks and gives us a fat blank array.
  4. 1 is in countArray land and through a cruel twist of fate, 1 is condemned to eternal nothingness afterall as he’s sitting in the some array having not been used because 0 got in the way and killed the function.

Much like a recursive function, I feel my brain in an endless loop here. God bless you folks at FCC for persevering…

I think the big thing you are missing is that multiple separate function calls occur.

When you call countup(1), you end up calling countup(0) in your else block. This means that execution of countup(1) pauses until the function call to countup(0) finishes. Once countup(0) returns it’s array, then countup(1) can resume.

Ok think I get that:

  1. 1 is checked to see if it’s less than 1. It isn’t so over to ‘else’…
  2. const countArray = countup(n - 1); here, 0 is n which returns the blank array. This function call is now done… back to the first function call resuming:
  3. countArray.push happens and pushes 1. So an array with [1] is returned.

Right. This back and forth is the piece that makes recursion work. countup(2) will wait for countup(1) to finish before it can finish.

With 2 as n:

**Function Call 1 starts **
N = 2. 2 is not less than 1

Pause Function Call 1

Function call 2 starts

N = 1. 1 is not less than 1
pause Function Call 2

Function Call 3 Starts
N= 0. 0 is less than 1, return [ ]. Because Function call 3 has reached it’s ‘return’ statement, it’s stopped…

Function Call 3 COMPLETED & STOPS - [ ] (blank array, the return code)

Function Call 2 resumes
the blank array - [ ] - is stored in countArray which then pushes 1 to the array. countArray is returned and because there is a ‘return’, this one’s done…

Function Call 2 COMPLETED & STOPS - [1] is returned to Function Call 1…

Function Call 1 resumes
countArray has [1] in it from the previous function call, the function proceeds…
2 is pushed to the array so it’s now [1, 2]. and then this final array [1, 2] is returned and that’s the final computation done!

Phew! Is that right?

1 Like

There you go!

1 Like

Ah great! Thank you so so much for your help and to everyone else on this thread. Really appreciate it.

1 Like

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