0

what are the steps used in this code to calculate the Fibonacci sequence?

let a = 0; let b = 1; for (let i = 1; i <= 10; i++) { let 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.

24th Mar 2019, 3:55 AM
Gabriel Junior
Gabriel Junior - avatar
1 Resposta
0
at each iteration, the variable b takes the value of the sum of a and b, and is also displayed on the screen. the variable a takes the value b, so after each iteration the variables a and b will take the values ​​(0,1) -> (1, 0+1) -> (1, 1+1) -> (2, 1+2) -> (3, 2+3) and so on
24th Mar 2019, 5:20 AM
Before
Before - avatar