What is recursion? In JavaScript, recursion is a way to solve a problem by calling a function from within that same function. While this may be considered one of the most complicated ideas in software development, the concept is really quite simple. As an example to illustrate this tutorial, we’ll write some JavaScript code implementing a function that calculates the Nth number of the Fibonacci sequence.
Three things to remember about recursion:
1. Always make progress towards a solution every time you recurse.
2. Always have a break point where the function stops calling itself.
3. If you don’t design your recursive function right, or if you overuse the method, you can exhaust your stack, and the program will run out of memory.
Copyright (c) 2013 Rodrigo Silveira http://www.easylearntutorial.com
Original source
6 responses to “Recursion & Fibonacci Sequence – JavaScript Tutorial for Beginners”
good video. Have been trying to understand recursion all day. I read a lot of explanations. You seemed to touch on the one and zero very briefly, but i had that eureka moment "oh… now i get it". Cheers
Great tutorial! Thank you!
What editor are you using?
Thank you very useful!
This might be a bit better: function fib(n) { return n < 2 ? n: fib(n – 2) + fib(n -1); }
Oops, didn't think about the size of that text. Thanks for pointing that out.
A debugger; statement would help for people new to JavaScript, I think.
Also, if you're using Chrome, at least, you can press CTRL and + to make the text bigger in the debugging window.