DEV Community

Rajiv Chaulagain
Rajiv Chaulagain

Posted on

Let's print fibonacci series with js with all explanation and problem solving.

Here, Problem solving is the only skill a programmer needs so I would like to show some case for it.
In above question we can find that we have to print fibonacci series,

Now fibonacci series looks like this :
1 , 1, 2 , 3 , 5 , 8 ......

Now, we have to print this fibonacci series so first thing we need is the pattern here , we find that the first number and second number while adding gives the third number.
For eg : 1 + 1 -> 2 , 1 + 2 -> 3 , 2 + 3 -> 5 .

woo we found the pattern half of the solution is done. Try solving this problem into some paper before writing on code.
Then start coding immediately

let a = 0;
let b = 1;
let c;
for (let index = 0; index < 10; index++) {
    console.log(b);
    c = a + b
    a = b 
    b = c
}
Enter fullscreen mode Exit fullscreen mode

Now what we have done .
1 -> Declaring variable a , b ,c
2 -> looping so that we can get how much interval or value we want
3 -> logging the first value as b that gives 1.
4 -> Now variable c adds two variable 1 and 0 in initial
5 -> a takes value of b and b takes value of c

Conclusion,
Hence this is about the problem solving not about coding

Top comments (0)