odin-js-fundamentals-part-5/10_fibonacci/solution/fibonacci-solution.js

35 lines
825 B
JavaScript
Raw Normal View History

2024-01-11 08:52:05 +00:00
const fibonacci = function(countArg) {
// checks argument's type and makes sure we use
// a number throughout rest of function.
let count
if (typeof countArg !== 'number') {
count = parseInt(countArg)
} else {
count = countArg
}
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;
};
// Another way to do it is by using an iterative approach with an array containing two values, 0 and 1.
// const fib = [0, 1];
// for (let i = 2; i <= count; i++) {
// fib[i] = fib[i - 1] + fib[i - 2];
// }
// return fib[count];
module.exports = fibonacci;