英文:
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;
}
这是代码的输出:
我的代码没有问题,我只想知道如何阻止它生成两个1。如果可能的话,你能告诉我解决方法吗?
我在论坛和Stack Overflow 上搜索过,但没有结果。
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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;
}
<!-- end snippet -->
This is the 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?
I've searched forums and stackoverflow but no results
答案1
得分: -2
这是Fibonacci数列:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...
如果你只想要一个 1
,那就不再被称为Fibonacci数列
了,然而,你可以这样做:
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;
}
英文:
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:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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;
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论