mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-07-19 06:33:24 +09:00
Merge pull request #370 from cats256/patch-2
Update fibonacci-solution.js
This commit is contained in:
@@ -1,10 +1,17 @@
|
|||||||
const fibonacci = function(count) {
|
const fibonacci = function(count) {
|
||||||
if (count < 0) return "OOPS"
|
if (count < 0) return "OOPS";
|
||||||
const fibPart = [0, 1];
|
if (count === 0) return 0;
|
||||||
for (let index = 1; index < count; index++) {
|
|
||||||
fibPart.push(fibPart[index] + fibPart[index -1]);
|
let firstPrev = 1;
|
||||||
}
|
let secondPrev = 0;
|
||||||
return fibPart[count];
|
|
||||||
|
for (let i = 2; i <= count; i++) {
|
||||||
|
let current = firstPrev + secondPrev;
|
||||||
|
secondPrev = firstPrev;
|
||||||
|
firstPrev = current;
|
||||||
|
}
|
||||||
|
|
||||||
|
return firstPrev;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = fibonacci;
|
module.exports = fibonacci;
|
||||||
|
|||||||
Reference in New Issue
Block a user