odin-default-js-exercises/10_fibonacci/solution/fibonacci-solution.js

18 lines
361 B
JavaScript

const fibonacci = function(count) {
if (count < 0) return "OOPS";
if (count === 0) return 0;
let firstPrev = 1;
let secondPrev = 0;
for (let i = 2; i <= count; i++) {
let current = firstPrev + secondPrev;
secondPrev = firstPrev;
firstPrev = current;
}
return firstPrev;
};
module.exports = fibonacci;