Fibonacci Recursive vs Iterative in JavaScript

Run Settings
LanguageJavaScript
Language Version
Run Command
// Given a number N return the index value of the Fibonacci sequence, where the sequence is: // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ... // the pattern of the sequence is that each value is the sum of the 2 previous values, that means that for N=5 → 2+3 //For example: fibonacciRecursive(6) should return 8 // O(2^n) function fibonacciRecursive(n) { if (n < 2) return n; let answer = fibonacciRecursive(n-1) + fibonacciRecursive(n-2); return answer; } // O(n) 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] } console.log(fibonacciRecursive(6)); console.log(fibonacciIterative(6));
Editor Settings
Theme
Key bindings
Full width
Lines