Basic JavaScript - Use Recursion to Create a Range of Numbers

Tell us what’s happening:
Describe your issue in detail here.

I’m confused with the understanding of the definition of recursive function.
Am I correct that inserted variables start with startNum and if (endNum < startNum) plays a role like a limitation of counting ?

Also I don’t see why we have to put only endNum after .push… :sob:

Solution

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

Link to the challenge:

A recursive function is just a function that calls itself.

If you want to teach someone to find a name in a phone book… Open the book at the halfway point. Is the name in the first half or the second half. Go to that half. Open that half halfway. Is the work in the first half or the second half? Go to that half…

That is a recursive algorithm - it calls itself.

The previous challenge had you basically create a range of numbers. The difference is that it was a range always starting with 1. That is because of the “base case”:

  if (n < 1) {
    return [];
  } else {

I haven’t done this, but I’m guessing you could mess around with that.

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