I am getting the correct output but not passing (recursion JS))

Tell us what’s happening:
Finishing up the last few problems on the Basics of JS coarse. I believe that my code is working correctly to pass the challenge yet is saying that my results are not correct. Any ideas why this is not working? Thanks

  **Your code so far**

function rangeOfNumbers(startNum, endNum) {
const range = [];

if(startNum <= endNum)
{

range.push(startNum);
console.log(range);
rangeOfNumbers(startNum+1,endNum); 

}
else{
console.log(range);
range.push(endNum)
}
 return range;
};
  **Your browser information:**

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

The tests are correct, your code is not working properly. One thing you can do is install node on your computer which will allow you to run your JS code locally at the terminal and then you can test for yourself whether your code is working.

Also, I would recommend you start by handling the base case first (in the if) and then add the recursion in the else. This is not required but it seems to make things a little clearer. So for the if, when are you going to stop making recursive calls and what are you going to return when that happens?

Hi there,
I think you don’t need to push the endNum argument if it is not bigger than startNum. The else section doesn’t make sense.

Also, instead of incrementing the startNum try decrement the endNum. This way you have an ending for the recursive call.

Note that you can add the returned argument of a recursive call into an variable. By this you directly push the variable into the array.

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