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: