Too much recursion

Too much recursion
0
function rangeOfNumbers(startNum, endNum) {
if (endNum<startNum){
  return [];
}

else{
  const myArray=rangeOfNumbers(startNum,endNum--);
  myArray.push(endNum);
  return myArray;
}


};
console.log(rangeOfNumbers(1,2));//return [1,2]

why this algroithm doesnot work , i tried cloning it from previous recursion exercise. i know there is base case but why does it say infinte recursion.:frowning:

That minus minus after the endNum variable is the culprit, When the -- is after the variable, the value passed to the function does not change until after the call is made. It is as if you called rangeOfNumbers(startNum,endNum). Think of another way to pass the value of endNum decremented by 1.

1 Like

thanks , i dont think i actually grabbed the inner concept but changing endNum-- to endNum-1 worked for me . WTJS :crazy_face: