mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-06-05 15:09:09 +09:00
26 lines
594 B
JavaScript
26 lines
594 B
JavaScript
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;
|
|
};
|
|
|
|
module.exports = fibonacci; |