JavaScript Example: Recursion

,
Want to master JavaScript in a month? Commit. Buy Xah JavaScript Tutorial. You also get Xah HTML Tutorial and Xah CSS Tutorial.

This page shows a example defining a factorial function, using recursion.

function ff(n) {
    if (n < 0)  { return -1; }

    if (n == 0) {
        return 1;
    }
    else {
        return (n * ff(n - 1));
    }
}

document.getElementById("id19564").onclick = function(){ alert(ff(5));};

to see result.

blog comments powered by Disqus