Recursion with two parameters

Tell us what’s happening:

Alright, so, I’m honestly following the example of an early exercise for recursion that returns and array of numbers counting up to n. So, I figured I could use endNum - 1 as the parameter that does the counting and solve the problem with answer.push(endNum);.

I get [] when if startNum is larger than endNum.
I return the [startNum] if both of the parameters are equal.
However, when I try rangeOfNumber(1, 5) the function only returns [5].

Your code so far


function rangeOfNumbers(startNum, endNum) {
if(startNum > endNum){
  return [];
} if (startNum = endNum) {
  return [startNum];
} else {
  const answer = rangeOfNumbers(startNum, endNum - 1);
  answer.push(endNum);
  return answer;
}
};

Your browser information:

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

So, I removed the second IF statement testing for startNum and endNum equality and I was able to return the correct answer for rangeOfNumbers(1, 5).

I solved it. No need to respond.

if you haven’t noticed yet, this is an assignment, not a comparison

crap, you’re right… forgot, that always trips me up.

But with that aside, does my explanation make sense in how I’m thinking about the problem? Whether I’m thinking about how it iterates through rightly or wrongly.

a thing that will not work is this:

in the sense that, or you start with a startNum bigger than the endNum or the first sentence is not gonna happen.
If you have startNum and endNum being equal, (once you fix that issue with the assignment operator), your function returns [startNum] and stop - there is not a new function called in which startNum is biggern than endNum

other than that you were doing pretty well