DEV Community

Rajiv Chaulagain
Rajiv Chaulagain

Posted on

4

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

AWS Q Developer image

Your AI Code Assistant

Implement features, document your code, or refactor your projects.
Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay