Javascript Example: Recursion

Advertise Here

, 2005-08

This page shows a example defining a factorial function, using recursion. If you are not familiar with js, see: Javascript Basics.

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

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

to see result.

blog comments powered by Disqus