I did not get this,plz help

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

  **Your code so far**

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

}

};
rangeOfNumbers(1,5);
  **Your browser information:**

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

1 Like

Hey there @RajeevP ! What exactly are you confused about? If you can be a bit more specific I think I can help explain :smiley:

Your answer is pretty great and actually similar to mine; keep up the good work!

Best,
Cy499_Studios

hey,actually i copied this code from someone else,i did not get this whole program,plz can u explain .

hey there @RajeevP !

Sorry for the late reply, but here’s the rundown of what your code is doing.

First I suggest you check out this article on recursion just to understand the concept:

Basically your setting a parameter with your first If statement. This is the recursive call that in your else statement is going to keep executing until startnum === 0 .

Take a look at my comments on your code:

function rangeOfNumbers(startNum, endNum) {
if(endNum - startNum === 0){
  return [startNum]; // If there is no endNum return the first number
}else {
// Works like a loop to continually push the numbers from startNum to endNum
  var arr = rangeOfNumbers(startNum,endNum - 1); // Starts at startNum but subtracts 1- from endNum
  arr.push(endNum); //pushes the number each time recursive function is called
  return arr; // return the final array 

}

};
rangeOfNumbers(1,5);

Hope I got this right LOL

Hope this helps!
Best,
Cy499_Studios

Here’s how the flow of recursion works. You keep calling the same function with different values fo endNum. At the end of recursive calls, the end case (startNum === endNum) becomes true and recursion stops. The answer is passed back to the caller, building the answer as you keep returning. In the diagram, I used the term ‘Concat’ but it’s a push method, arr.push(endNum).

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