Basic JavaScript: Use Recursion to Create a Range of Numbers (I do not understand how it works)

Tell us what’s happening:
I do not understand how it works. When I call the rangeOfNumbers function than how it comes that the numbers.push and return works. Can someone please explain it to me.

Your code so far


[spoiler]

function rangeOfNumbers(startNum, endNum) {
 if (startNum - endNum == 0) {
  return [startNum];
} else if (startNum <= endNum) {
  var numbers = rangeOfNumbers(startNum, endNum - 1);    
  numbers.push(endNum);
  return numbers;
}
};
[/spoiler]

Your browser information:

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers

2 Likes

what do you mean how push and return works?
push is an array method, it add its argument(s) to the end of the array it is used on.
return is used to set what’s the output of the function


recursion means that the function call itself

inside the function, there is the call rangeOfNumbers(startNum, endNum -1)

the function is called over and over till the condition that stops the recursion is met

1 Like

This is the point when the function runs itself right:
var numbers = rangeOfNumbers(startNum, endNum - 1);
So, when the code above runs itself how the codes below start to run?

numbers.push(endNum);
  return numbers;

I mean it is kind of loop right how it reaches the code below? (the push and return code)

the function keeps calling itself till it reaches this, then the last function call has an output, which is then used to the second last to give a value to the numbers variable, then the array has the push method called on, and is then outputted by the function, the third to last call uses this array to give a value to his numbers variable and so on

3 Likes
function rangeOfNumbers(startNum, endNum) {

  if(startNum > endNum)

  {

    return [];

  }

  else {

    const rangeArray = rangeOfNumbers(startNum,endNum-1);

    rangeArray.push(endNum);

    return rangeArray;

  }

};

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like