Beware of stack overflow error (no, it's not a site, it is an error). So the recursion really good at small repetition. However, it might overwhelm the computer if too much repetition, can ended up program to crash.
stack overflow
The solution? Use while loop
Before:
function decrement(n) { if(n > 0) { return decrement(n-1); } else { return n; } } console.log(decrement(10));
After:
function decrement(n) { while(n > 10) { n -= 1; // This can be n--, actually } return n; } console.log(decrement(10));
Thanks!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Beware of
stack overflow
error (no, it's not a site, it is an error). So the recursion really good at small repetition. However, it might overwhelm the computer if too much repetition, can ended up program to crash.The solution? Use while loop
Before:
After:
Thanks!