Counting Rabbits (Calculating the Fibonacci Series Using a Recursive Function) : Math : Development : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
PHP
JavaScript DHTML Home »  Development   » [  Math  ]   
 



Counting Rabbits (Calculating the Fibonacci Series Using a Recursive Function)

Please note that some example is only working under IE or Firefox.

/*

Learn How to Program Using Any Web Browser
by Harold Davis 

Apress CopyRight 2004

ISBN: 1590591135
*/
<HTML>
<HEAD>
<TITLE> 
Counting rabbits... 
</TITLE>
<SCRIPT
    function fib (inNum) { 
       if (inNum == 0
          var fibNum = 0
       else 
           if (inNum == 1
              fibNum = 1
           else  
                // recursive function call 
                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" rabbit pairs<br>")
     
   
</SCRIPT>
</HEAD>
<BODY>
<FORM Name="theForm">
<TABLE cellspacing=5>
<TR>
<TD>
<INPUT Type=Text Name="numFibs">
<TD>
<INPUT Type=Button Value="Show Fibs" onClick='writeFibs(numFibs.value);'>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>


           
       
Related examples in the same category
1.  Demo all math methods
2.  Math Round: round a number to the nearest whole number
3.  Max number: get max number from two inputs
4.  Min number: get the min number from two inputs
5.  Using the Math Object
6.  Using the const Keyword
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.