odin-javascript-exercises/10_fibonacci/fibonacci.js
2023-06-19 17:15:07 -07:00

19 lines
384 B
JavaScript

const fibonacci = function (index) {
if (index < 0) return 'OOPS'
// in loop, add up the sequence until the length of the array is index - 1
let seq = [1]
while (seq.length < index) {
if (seq.length === 1) {
seq.push(1)
} else {
seq.push(seq.at(-1) + seq.at(-2))
}
}
return seq.pop()
}
// Do not edit below this line
module.exports = fibonacci