Basic JavaScript - Use Recursion to Create a Range of Numbers

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

Cant understand whats wrong with this code …PLEASE HELP!
Your code so far

function rangeOfNumbers(startNum, endNum) {
var arr=[]
if(startNum===endNum) arr.push(startNum);
else {arr.push(startNum)
     rangeOfNumbers(startNum+1,endNum)
}
return arr;
};
  **Your browser information:**

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

Challenge: Basic JavaScript - Use Recursion to Create a Range of Numbers

Link to the challenge:

Let’s start with some formatting so we can see what is going on

function rangeOfNumbers(startNum, endNum) {
  var arr = [];
  if (startNum === endNum) {
    arr.push(startNum);
  } else {
    arr.push(startNum);
    rangeOfNumbers(startNum + 1, endNum);
  }
  return arr;
};
  1. don’t use var - it is a legacy feature that shouldn’t be used

  2. you are not using the return value of the recursive function call anywhere

1st point , noted , thank you !
2nd point…can you help by modifying my code by what you mean please?

You call rangeOfNumbers, but what is that, and what are you doing with it?

1 Like

my aim is to return an array when I call rangeof numbers with the gieven parameters.

Right… But you are not using the return value of the recursive function call.

The array that was returned is thrown away instead of stored and used.

Got it !..Thanks a lot…Had a feeling this was were I made the mistake!!

1 Like