Issue
I have written a code for fibbonaci series using arrow function....But is too complex... Is there any other way to find the fibbonaci series using arrow functions only??
var n = Number(prompt("Enter limit: "));
const fibonacci = n =>
Array.from({
length: n
}).reduce(
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []
);
console.log("Fibonnaci series: ");
console.log(fibonacci(n));
Solution
You can use recursion to build the sequence from bottom up:
fibs = n => fibs2([0, 1], n - 2)
fibs2 = (a, n) => n === 0 ? a
: fibs2(a.concat(a[a.length - 2] + a[a.length - 1]), n - 1)
console.log(...fibs(50))
Answered By - georg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.