fibonacci

Run Settings
LanguageJavaScript
Language Version
Run Command
/** * Given a number N return the index value of the Finonacci * Sequence, where the sequence is: * * 0,1,1,2,3,5,8,13,21,34,55,89,144... * The pattern of sequence is that each value is the sum of the * 2 previous values, that means that for N=5 -> 2+3 */ function fibonacciIterative(n) { let fib = [0, 1]; for (let i = 2; i <= n; i++) { fib.push(fib[i - 2] + fib[i - 1]); } return fib[n]; } console.log(fibonacciIterative(6)); function fibonacciRecursive(n) { if (n === 1 || n === 2) { return 1; } if (n === 0) { return 0; } return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2); } console.log(fibonacciRecursive(6));
Editor Settings
Theme
Key bindings
Full width
Lines