| Viewed 60 times

How to Count Fibonacci Sequence in JavaScript?


Examples 0, 1 ➞ 1 1, 1 ➞ 2 1, 2 ➞ 3

1 Answer
Mohammad Qandeel
Answered
16
function fibonacciSequence() {
 const sumOfLastTwo = ar => ar.slice(-2).reduce((a,v) => a + v, 0);

 let fib = [0, 1];
 while (sumOfLastTwo(fib) < 255) { fib.push(sumOfLastTwo(fib)); }
 return fib;
}


Related Questions