Why does the increment/decrement operator cause bugs when used in a recursive function?

Hi @WaningJupiter,

Sorry, but isn’t that obvious? Just joking :slight_smile:

n-- stands for n=n-1

After each operation of n--, n is changing and you doing it twice in your code. So, for the first call of drawBranch, ‘n’ is used and changed and on the second call it uses that changed value (and change further).
Your solution is the proper way to do in this case! :+1:

Hope that helps to clarify…

Cheers
— mnse

PS: this is not related to recursion, you would have the same in other cases.

EDIT: add JavaScript doc

Actually, for java (but good explanation though)
Increment (++) and Decrement (–) Operators in Java Explained

3 Likes