Basic JavaScript - Use Recursion to Create a Range of Numbers- HELP understanding a tiny part of it

Hey, so I actually solved this without hints or looking at the solution, BUT although I typed it out, I don’t fully understand how a certain part of it works. The part is the:

(startNum, (endNum - 1))

So, I had the (endNum - 1) there for a while, however, I realized that this needed two arguments passed in, so I added the "startNum, " in there, but I do not know how that works into the math and recursion of it all. I am even having difficulty on describing what specifically i don’t understand about it, but I will try a bit more…

I’m using the endNum - 1 part of it to run over and over again to give me the numbers i need until the base case is activated, but what the heck does the startNum there with a comma (,) do for this? I would think it would mess up the recursion math of it.

The best example i can come up with to clarify what I’m asking is if we have an equation along the lines of this:

x++ until we hit 500!,

and then we throw in a startNum into it like

startNum , x++ until we hit 500!,

it just completely screws up anything going on, throwing in a number that doesn’t have anything to do with the equation into mix.

Eh, I know this may be hard to understand but I’ll attempt to restate one last time:

The logic and math and recursion seem to work perfectly with just endNum - 1 being in the parenthesis, so how in the world does throwing in the startNum not screw it up?

If you can explain specifically the steps the recursion uses and somehow includes the startNum , i’d really appreciate it.

By the way, the way this guy explains recursion is why I was able to conceptualize every part of this, EXCEPT for the part I’ve mentioned in this post.

I really appreciate any help in understanding this!!

Your code so far

function rangeOfNumbers(startNum, endNum) {
  if (endNum < startNum) {
      return [];
  } else {
       let rangeArray = rangeOfNumbers(startNum, (endNum - 1));
       rangeArray.push(endNum);
       return rangeArray
  }





};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0

Challenge: Basic JavaScript - Use Recursion to Create a Range of Numbers

Link to the challenge:

Hi,

So just to confirm, you are wondering why you continue to call rangeOfNumbers(startNum, endNum - 1) instead of just rangeOfNumbers(endNum - 1) inside the else block?

If so, then I think that the issue is that you may be thinking that the rangeOfNumbers(A, B) call is different from the rangeOfNumbers(A, B) declaration that it is inside of. In fact, they are both the very same function object. Being the same function, they will accept the same arguments. So we call rangeOfNumbers (startNum, endNum) instead of just rangeOfNumbers(endNum - 1) (else, on the next recursive call if (endNum < startNum) { would cause an error since startNum will be undefined.

Well, sort of. What you’re saying makes sense and it makes sense that when we call “rangeOfNumbers” again, it needs both arguements just like it was initially given.

But my issue is with the calculation & output of the range of numbers. I did my calculations with solely “(endNum - 1)” in there and the calculations come out perfectly, I thought. So, when we add “startNum”, it just seems like that would throw off my calculations in a way that I’m not even sure of.

It’s kind of a big ask here, but the explanation in this video is how i do the math/calculations for what’s happening. I realize this is a countup video, but I found very similar logic to apply here if I changed it a bit, and again, it seems to work perfectly with the (endNum - 1) part there but I don’t understand how startNum fits into the calculations, even though I know it needs to be there to match the original number of arguements.

Is it something like, the calculations for recursion can somehow ignore the first arguement in this case? I’m really not sure, my head hurts here. Thank you for any time spent on this though, and sorry if my question doesn’t make sense.

You cannot ignore the first argument, no.

Recursion is using function calls. Those function calls are all separate. So the function call rangeOfNumbers(startNum, (endNum - 1)) will create an array with numbers between startNum and endNum - 1. Then your next line adds the endNum to that array so that its a range between startNum and endNum.

thank you for your explanation!

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