Fibonacci Iterative
Print out the n-th entry in the fibonacci series. The fibonacci series is an ordering of numbers where each number is the sum of the preceeding two.
Example:
fibonacciIterative(8) 21
function fibonacciIterative(n) {
let arr = [0,1]
for(let i = 2; i < n + 1; i++) {
arr.push(arr[i-2]+ arr[i-1])
}
return arr[n]
}
fibonacciIterative(8) 21
We create a function fibonacciIterative with parameter of number n and variable arr=[0,1]
We loop with for loop starting from 2 since we have the first two numbers in the result array, we iterate until is less or equal to n and i++
We use variable array arr and do arr.push(arr[i-2] + arr[i-1])
push method adds item in the array and inside this push we have last number from the for loop and the one before that added
In the end we return return arr[n]