Question about Basic JavaScript: Use Recursion to Create a Range of Numbers

Tell us what’s happening:
In the lesson: Basic JavaScript: Use Recursion to Create a Range of Numbers, i am confused that why the endNum need to minus 1 in var number.

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 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

the value with wich the function is called needs to change so that eventually it will reach the base case and stop the recursion, in this case who wrote this algorithm choose to reduce by one endNum at each call. It is not the only way in which this can be done.

3 Likes

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, 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.

Thank you.

1 Like