Recursive function
|
|
<HTML> <HEAD> <SCRIPT> function fib (inNum) { if (inNum == 0) var fibNum = 0; else{ if (inNum == 1) fibNum = 1; else{ fibNum = fib(inNum - 2) + fib(inNum - 1); } } return fibNum; }
function writeFibs(topFib) { for (var i=0; i <= topFib ; i++) { document.write ("Fib(" + i + ") = " + fib(i) + " <br>"); } } </SCRIPT> </HEAD> <BODY> <FORM Name="theForm"> <INPUT Type=Text Name="numFibs"> <INPUT Type=Button Value="Show Fibs" onClick='writeFibs(numFibs.value);'> </FORM> </BODY> </HTML>
|
|
|
HTML code for linking to this page:
Related in same category :
-
-
-
-
|