Can anyone explain what's happening?

Can anyone explain what’s happening in this function? camperbot provided no explanation. Thanks!

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

This is a recursive function. That is a confusing topic and can be very frustrating for learners. Rather than go through a long and complicated explanation, I would suggest using the search function in the upper right and search for “rangeOfNumbers” and you will find hundreds of discussions of this function, some of them with detailed explanations. If you’re still confused after that, check back.

1 Like