Basic JavaScript - Use Recursion to Create a Range of Numbers

Tell us what’s happening:

Local variables would be better for caching arrays than global variables, but I’d like to know which is the global variable. And my function should return an array, but does rangeOfNumbers() provide an array?

Your code so far

function rangeOfNumbers(startNum, endNum) {
  if (endNum - startNum === 0) {
    return startNum;
  } else {
    let 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/129.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Use Recursion to Create a Range of Numbers

You don’t have a global variable in the posted code

You aren’t returning an array here but this function must return an array

Here’s the code:

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

I just changed the top part so that I’m returning an array.

Ok. Do you have further questions?

I just wanted to check the array syntax if that’s good or not.

You are in fact returning an array here, if that’s what you are asking?

Never mind, I should’ve put endNum < startNum, and inserted the return statement inside the if statement.