2023-05-20 06:22:12 +00:00
|
|
|
const fibonacci = function(count) {
|
2023-07-04 17:07:04 +00:00
|
|
|
if (count < 0) return "OOPS";
|
|
|
|
if (count === 0) return 0;
|
|
|
|
|
2023-07-05 14:15:36 +00:00
|
|
|
let firstPrev = 1;
|
|
|
|
let secondPrev = 0;
|
2023-07-04 17:07:04 +00:00
|
|
|
|
2023-07-05 14:15:36 +00:00
|
|
|
// For clarification: curr stands for current. This is standard syntax
|
|
|
|
|
2023-07-04 17:07:04 +00:00
|
|
|
for (let i = 2; i <= count; i++) {
|
2023-07-05 14:15:36 +00:00
|
|
|
let curr = firstPrev + secondPrev;
|
|
|
|
secondPrev = firstPrev;
|
|
|
|
firstPrev = curr;
|
2023-07-04 17:07:04 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 14:15:36 +00:00
|
|
|
return firstPrev;
|
2022-02-20 19:07:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = fibonacci;
|