Recursion ! why doesnot this work

A good recursive function should not change anything’s state, such as shifting or popping from an array. Instead, you want to return and/or pass new values instead. Like using arr.slice(1) instead of arr.shift() (and that’s a pattern you’ll see a lot in recursive functions that work with arrays). When you do that, you can use the substitution model, which I explain here using factorial as the recursive function.

1 Like