Javascript Recurrsion

Tell us what’s happening:

Your code so far


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


How does the program know var num = rangeOfNumbers(startNum, endNum - 1); num should be a list and startNum is the first item and endNum the last item of it.

Your browser information:

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

Hello there,

I am not sure exactly what you are asking, because the program does not know…you tell it.

// Define the function
function rangeOfNumbers(startNum, endNum) {
//// Do stuff
}

// Call the function, telling it 'startNum === 1', 'endNum === 6'
rangeOfNumbers(1, 6)

This is the call stack for your program, you can see how your recursive call behaves:

Follow the blue arrow to see the state for each function call.



Nothing magic. Play with the default debugger provided by the browser and eventually it will click.