Skip to content

Commit 0692f23

Browse files
author
wakidurrahman
committed
feat: fibonacci
1 parent 86a63e3 commit 0692f23

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/code-challenges/code-challenges-002-010.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,18 @@ exports.iterative = function (array) {
193193
module.exports = function (a, b) {
194194
return a.length === b.length && (a + a).indexOf(b) > -1;
195195
};
196+
197+
/**
198+
* Q009: Return the N-th value of the Fibonacci sequence. Solve in O(n)
199+
*/
200+
201+
// The easiest solution that comes to mind here is iteration.
202+
203+
function fibonacciNumbers(n) {
204+
let arr = [0, 1];
205+
206+
for (let i = 2; i < n + 1; i++) {
207+
arr.push(arr[i - 2] + arr[i - 1]);
208+
}
209+
return arr[n];
210+
}

0 commit comments

Comments
 (0)