Fibonacci Sequence in Javascript

I am very new to programming in general, and itโ€™s hard for me to understand this example of a Fibonacci sequence:

var fib = [0, 1]; for (var i = 2; i < n; i++) { fib[ i ] = fib[ i - 1 ] + fib[ i - 2 ]; console.log(fib); } 

At the first iteration, index 2 is 1, which is quite simple. But, when I try to execute the second iteration with i = 3, I get:

 fib[ 3 ] = fib[ 3 - 1 ] + fib[ 3 - 2 ]; fib[ 3 ] = fib[ 2 ] + fib[ 1 ]; fib[ 3 ] = fib[ 3 ]; 

Where am I mistaken in my thoughts? So far I:

 var fib = [0,1,1,3] 

which, as I know, is wrong.

+7
source share
6 answers

When you talk about code, you make the transition from fib[3] = fib[2] + fib[1] to fib[3] = fib[3] . This occurs as a transformation that leads to a correct statement, but it is not how it works. This code adds the value at index 2 to the value at index 1 . This is not the same as taking a value in index 3 . The way this reasoning should work is as follows:

You start with fib = [0, 1] . Then in the first iteration of the loop you have fib[2] = fib[1] + fib[0] . This means that you add the value at index 0 (which happens to be 0 ) to the value at index 1 (which happens to be 1 ) to get the value that you put at the end of array ( 1 ). Then in the second iteration, you do a similar thing, adding the value in index 1 (another 1 ) to the value in index 2 (also 1 ) to get 2 , which goes to the end of the array. This continues, and at each iteration, you add the last two values โ€‹โ€‹to the array to get the next value.

In JavaScript, when an array of type fib , fib[i] refers to the i th value in this array, starting from 0 . So fib[0] is the first element in the array, fib[1] is the second element in the array, etc.

+7
source
 fib[ 3 ] = fib[ 3 - 1 ] + fib[ 3 - 2 ]; fib[ 3 ] = fib[ 2 ] + fib[ 1 ]; fib[ 3 ] = fib[ 3 ]; 

You add indexes up, not the value in the array, the pointer points to

 fib[ 3 ] = fib[ 3 - 1 ] + fib[ 3 - 2 ]; fib[ 3 ] = fib[ 2 ] + fib[ 1 ]; fib[ 3 ] = 1 + 1; [0,1,1,2] fib[0] = 0 fib[1] = 1 fib[2] = 1 fib[3] will equal 2 

So the next iteration

 fib[4] = fib[4-1] +fib[4-2] fib[4] = fib[3] + fib[2] fib[4] = 1 + 2 fib[4] = 3 
+2
source

You code is ok. Ran received the correct conclusion:

 var fib = [0, 1]; for (var i = 2; i < 10; i++) { fib[ i ] = fib[ i - 1 ] + fib[ i - 2 ]; console.log(fib); } 

Console: 0,1,1,2,3,5,8,13,21,34

+1
source
 fib[ 3 ] = fib[ 3 - 1 ] + fib[ 3 - 2 ]; fib[ 3 ] = fib[ 2 ] + fib[ 1 ]; fib[ 3 ] = fib[ 3 ]; 

So, your problem is in line # 2. The numbers in brackets [] are indexes that indicate which part of the array should be read. When the + operator is used, they are not added, but instead, two fib[2] and fib[1] are evaluated with their corresponding data, that is, they are calculated as 1 and 1 , which are then combined into 2 .

I hope this is clear. If not, just ask in the comments.

0
source

Ok why did you do it in your calculation

 fib[3] = fib[2] + fib[1] fib[3] = fib[3] 

When you are dealing with arrays, the value in the index represented by the sum of the indices is not equal to the sum of the values โ€‹โ€‹of the array at these indices.

Simply put, you cannot add numbers in square brackets on paper. This is similar to having two wallets with 5 and 15 dollars each in them. You canโ€™t say that you have $ 15, because 1 + 1 - two, and you look into the second wallet. Instead, you add the โ€œcontentsโ€ of the wallets to find the total amount of money. Which is 20

0
source

Implementation with array reduce()

 var a = [3,2,3,6,2,3,4]; var fib = a.reduce(function(sum, v, i) { return i === 0 ? sum.concat(v) : sum.concat(v + sum[i-1]); },[]); console.log( fib ); 

ES6:

 var fibES6 = a.reduce( (sum, v, i) => i === 0 ? sum.concat(v) : sum.concat(v + sum[i-1]) , []); console.log( fibES6 ); 
0
source

All Articles