Update fibonacci-solution.js
This commit is contained in:
parent
157972d135
commit
908c4ed26e
|
@ -1,17 +1,12 @@
|
||||||
const fibonacci = function(count) {
|
const fibonacci = function(count) {
|
||||||
if (count < 0) return "OOPS";
|
if (count < 0) return "OOPS";
|
||||||
if (count === 0) return 0;
|
if (count === 0) return 0;
|
||||||
|
|
||||||
let firstPrev = 1;
|
|
||||||
let secondPrev = 0;
|
|
||||||
|
|
||||||
|
const fib = [0, 1];
|
||||||
for (let i = 2; i <= count; i++) {
|
for (let i = 2; i <= count; i++) {
|
||||||
let current = firstPrev + secondPrev;
|
fib[i] = fib[i - 1] + fib[i - 2];
|
||||||
secondPrev = firstPrev;
|
|
||||||
firstPrev = current;
|
|
||||||
}
|
}
|
||||||
|
return fib[count];
|
||||||
return firstPrev;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = fibonacci;
|
module.exports = fibonacci;
|
||||||
|
|
Loading…
Reference in New Issue