Weird Solution for Use Recursion to Create a Range of Numbers

Tell us what’s happening:
when I was tackling this challenge, it took me a while to understand the flow of execution and I just want to confirm whats going with my code.
So at first, the function is called , parameters are passed , it steps over first condition because its false. The else part calls the function. At this point the recursive loops start and it keeps repeating it self till startNum reaches the same value as endNum. this is when the loop is broken and an array with startNum=10 is returned. however, startNum that called the function was valued at 9 and so 9 got unshifted to the front of the list. The rest of the function calls before this repeated until 5 was reached.
and numArray was returned.
Can anyone confirm that this is actually what is hapenning with the code?

  **Your code so far**

function rangeOfNumbers(startNum, endNum) {
if (startNum === endNum){
    return [startNum];
}else {
    const numArray = rangeOfNumbers(startNum+1,endNum)

    return numArray
  **Your browser information:**

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

Hello @hubertaslol if you have any extensions, please disable them. Better you work on Chrome.
Hope that helps!

The code works fine. I just want to confirm the flow of execution to make sure I understand what I wrote.

Your explanation sounds good to me and your code works. Unless you have a specific question about your code I’m not sure what else to say?

Thanks. I just needed a sanity check

I would guess that most people take the exact opposite approach and decrement endNum in the recursive call and then push endNum onto the local array. If you look at the solutions in the hints that’s how all of them do it. But your solution works just as well.

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