I want to divide an array into subarrays using recursion. The error i get is maximum call stack size exceeded. But there is a base case. I tried two base cases, but the error is the same

When start === arr.length-1 && end === arr.length

x.length === arr.lenght

const rec =(arr, index )=>{
let x =
let start = 0
let end= index
if ( start === arr.length-1 && end === arr.length){
return x
}

Agreeing with what Jeremy is saying, but scanning over your code:

if ( start === arr.length-1 && end === arr.length)

Are you sure about your math there? Are you sure about the logic? Do both conditions need to be met?

If I’m right, then I guess I can guess that you didn’t put a log statement in there. That would have been my first instinct. Successful programmers are the ones that want to dig in and figure out what the problem is. Put some log statements in there to see if you can figure out what the problem is.

Thanks a lot for the advice. I think the problem is that everytime the function runs, a brand new variables are created. I will take into consideration your suggestion about math.