Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript JS Fibonacci querie

andrzej1234

New Coder
Hi, can anyone explain why and how the number '2' is not skipped in the fib sequence?

const fib = [0,1];

for(let i=0; i < 18; i++) {

let nextfib = fib + fib[i +1]
fib.push(nextfib);
}

console.log(fib);

When I'm trying to figure out the steps I'm missing number 2 in the fib sequence
 
Hi, can anyone explain why and how the number '2' is not skipped in the fib sequence?

const fib = [0,1];

for(let i=0; i < 18; i++) {

let nextfib = fib + fib[i +1]
fib.push(nextfib);
}

console.log(fib);

When I'm trying to figure out the steps I'm missing number 2 in the fib sequence
Hi there,
you are not grabbing the previous value, you are just adding to the array itself.
try this
1697914072648.png
 
Hi!Have a good day!I dont know anything about JS but algorithms such as the Fibonacci sequence which have a very obvious base case and a recursion step are much easier to be written down as a recursive function.
 
Hi!Have a good day!I dont know anything about JS but algorithms such as the Fibonacci sequence which have a very obvious base case and a recursion step are much easier to be written down as a recursive function.
Hi there,
Could you provide some sort of guide for the user to follow if they wish to go down that path?
 
Hi there,
Could you provide some sort of guide for the user to follow if they wish to go down that path?
I will write my code in C and wish that the OP understands C:

C:
int fibonnaci(int n)
{
   
  
    if(n==0)
    {
       printf("0 : 0");

        return 0;
      
        //first base case//
    }
    else if(n==1)
    {
        printf("1 : 1");

        return 1;
      
        //second base case//
    }
    else
    {
        int y = fibonacci(n-1)+fibonacci(n-2);
      
        // recursion step //
      
        printf("%d : %d",n,y);
      
        return y;
    }
  
  
  
  
}
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom