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

18 lines
361 B
JavaScript
Raw Normal View History

const fibonacci = function(count) {
if (count < 0) return "OOPS";
if (count === 0) return 0;
2023-07-05 14:15:36 +00:00
let firstPrev = 1;
let secondPrev = 0;
for (let i = 2; i <= count; i++) {
2023-07-06 14:47:20 +00:00
let current = firstPrev + secondPrev;
2023-07-05 14:15:36 +00:00
secondPrev = firstPrev;
2023-07-06 14:47:20 +00:00
firstPrev = current;
}
2023-07-05 14:15:36 +00:00
return firstPrev;
2022-02-20 19:07:44 +00:00
};
module.exports = fibonacci;