Skip to content

Commit

Permalink
[Fibonacci Series] StephenGrider#12 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett Dawidowski committed Apr 4, 2019
1 parent 31d5c6c commit 36cdd41
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion exercises/fib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@
// Example:
// fib(4) === 3

function fib(n) {}

// O(n)
function fib(n) {
// Always know the first two will be 0, 1
const results = [0, 1];

// Increase next item by adding the two pervious items together [5, 8, 13]
for (let i = 2; i <= n; i++){
let a = results[i - 1];
let b = results[i - 2];

// Add together and append to array
results.push(a + b);
}
return results[results.length - 1];
}


// O(n)
function fib(n) {
// n = 0 or n = 1
if (n < 2) return n;

return fib(n - 1) + fib(n - 2);
}

module.exports = fib;

0 comments on commit 36cdd41

Please sign in to comment.