Basic JavaScript - Use Recursion to Create a Range of Numbers

Tell us what’s happening:

This solution worked. I’m just looking for some feedback on the comments I have included to see if my thinking is on the right track. I feel like I’m doing more learning of what to do rather than how to do with this course. So, I’m trying to add comments to each thing I do as a way to talk myself through it in a way that makes logical sense.

Your code so far

function rangeOfNumbers(startNum, endNum) {
  // "The starting number will always be less than or equal to the ending number"
  // create the condition that will end the recursion
  if (startNum >= endNum) {
  // "The function should return an array of integers which begins with a 
  // number represented by the startNum"
  // when this condition is met, create an array with the startNum
    return [startNum];
  } else {
  // I really just looked at 'Use recursion to create a countdown' exercise for this 
  // part and just stumbled onto it by replacing arguments in rangeOfNumbers similar
  // to the way it was done in the exercise.
  // I'm not sure if I really understand it. 
  // if condition not met, create a variable that will check conditions 1 by 1
  // reducing endNum by 1 until condition is met.
      const numArray = rangeOfNumbers(startNum, endNum - 1);
  // then start adding the endNums from the stack into the array
  // ex. rangeOfNumbers(1, 5) 1 !>= 5 so, then it tries (1, 4(endNum -1)) etc.
  // until it reaches (1, 1). at that point it creates an array of [1(startNum)]
  // then it will add endNum from each attempt to array (1, 2), (1, 3), (1, 4), (1, 5)
  // until it has exhausted the stack.
  // then return that array.
      return numArray;
  // I somewhat understand that recursions create a call stack that is then returned
  // using last in first out.

Your browser information:

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

Challenge Information:

Basic JavaScript - Use Recursion to Create a Range of Numbers

1 Like