Basic Javascript - Use Recursion to Create a Range of Numbers

Hi there, recursion is hard to understand. I solved the exercise but I’m not happy with my solution and I would like to know how you will solve same exercise using recursion.


My Solution:

function rangeOfNumbers(startNum, endNum) {
  if(startNum > endNum){
      return [];
      const range = rangeOfNumbers(startNum + 1, endNum);
    	return range;


What is


doing for you?

1 Like

Hi @JeremyLT ! Thanks for reply.
I added this increment operator after range.unshift() because I need to put the initial startNum value as first element of array. But I’m not sure if my solution is the best, I would like to read another solutions.

My bad. I tried again and I see the increment operator is unnecessary.

Thanks @JeremyLT

1 Like