简体   繁体   English

此函数中用于计算斐波那契数列的步骤是什么?

[英]What are the steps used in this function used to calculate the Fibonacci sequence?

 let a = 0; let b = 1; for (let i = 1; i <= 10; i++) { var c = a + b; a = b; b = c; console.log(c); }

So I have this little code that apparently works and that gives the sequence of Fibonacci but I still don't understand how the code does its thing so I think need a visual representation with numbers to understand.所以我有这个显然有效的小代码,它给出了斐波那契数列,但我仍然不明白代码是如何做的,所以我认为需要一个带有数字的视觉表示来理解。

Work through it step-by-step:逐步完成它:

let a = 0;
let b = 1;

Self-explanatory variable assignment.不言自明的变量赋值。

for (let i = 1; i <= 10; i++) {...}

Iterate from 1 to 10 with i storing the current iteration110迭代, i存储当前迭代

var c = a + b;

Make a variable c set to a + b .将变量c设置为a + b

a = b;
b = c;

Set a to b and b to c - makes new base values for next iteration.a设置为b并将bc - 为下一次迭代生成新的基值。

This example shows all the variables:此示例显示所有变量:

 let a = 0; let b = 1; for (let i = 1; i <= 10; i++) { var c = a + b; a = b; b = c; console.log(`a: ${a}, b: ${b}, c: ${c}, i: ${i}`); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM