Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    recursive fibanici

    <html><head>

    <title>a</title>
    <script text="javascript/text">
    document.write("the fibanic series is as follows" )
    var i;
    for (i=0; i<6; i++){ document.write(i);
    value=i;
    a(value);
    }
    function a(value){if(value===0){
    document.write("1");}
    if(value===1){
    docuument.write("1");}
    else
    document.write(a(value-1)+a(value-2));}

    </script>
    </head>
    <body></body>
    </html>
    i used the below written code but the the result that is coming is :
    the fibanic series is as follows00

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    HINT: There is a HUGE difference between RETURNING a value from a function and doing document.write() of the value inside the function.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,642
    Thanks
    6
    Thanked 1,005 Times in 978 Posts
    And it’s called Fibonacci, not fibanici.

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    docuument
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,924
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    <script text="javascript/text">



    Code:
    <script type = "text/javascript">
    
    var r = [1], n = 1, a = 0, b = 1, next; 
    function nextFibonacci() { 
    next = a + b; 
    return b = (a = b, next); 
    } 
    while (n++ < 12) { 
    r.push(nextFibonacci()); 
    } 
    
    alert (r); // 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 
    document.write(r);
    
    </script>
    Or simpler:-

    Code:
    <script type = "text/javascript">
    
    var fib = [];
    fib[0]=1; 
    fib[1]=1; 
    var numFib = 12;  // calculate first numFib numbers
    for (var c=2; c<=numFib; c++) { 
    fib[c] = fib[c-1] + fib[c-2]; 
    } 
    alert ("First " + numFib + " Fibonnacci numbers:- " + fib);
    document.write("First " + numFib + " Fibonnacci numbers:- " + fib);
    
    </script>
    Last edited by Philip M; 10-01-2012 at 08:54 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •