Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript: How do I prevent my Fibonacci generator from producing two "1"?

let sayi3 = 0, sayi4 = 1, nextTerm;
let number = 10;

console.log("Girdiğiniz sayının sırasına kadar olan Fibonacci değerleri:");

for (let i = 1; i <= number; i++) {
    console.log(sayi3);
    nextTerm = sayi3 + sayi4;
    sayi3 = sayi4;
    sayi4 = nextTerm;
}

This is the output:

output

There is nothing wrong with my code, I just want to know how can I stop it from generating two 1’s. Can you tell me the solution if possible?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve searched forums and stackoverflow but no results

>Solution :

This is Fibonacci sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...

If you want only one 1, that’s not called Fibonacci sequence anymore, however, you can do it this way:

let sayi3 = 1;
let sayi4 = 2;
let nextTerm;
let number = 10;
  console.log(0)
for (let i = 2; i <= number; i++) {
    console.log(sayi3);
    nextTerm = sayi3 + sayi4;
    sayi3 = sayi4;
    sayi4 = nextTerm;}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading