DEV Community

Discussion on: Welcome Thread - v51

Collapse
 
brandonmcconnell profile image
Brandon McConnell

Hey @aelmosalamy , I found your fibonacci one-liner solution on Reddit while building the exact same thing. Yours is great! I wanted to achieve this myself without recursion or default parameters, and this is what I came up with:

const fib = n => n > 3 ? [0,1,1,...Array(n-3).fill()].map((e,i,a) => a[i-2] ? ((a[i] = a[i-2] + a[i-1]) || a[i-2] + a[i-1]) : e) : [0,1,1].slice(0,n);

fib(0);  // []
fib(1);  // [0]
fib(3);  // [0, 1, 1]
fib(10); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Enter fullscreen mode Exit fullscreen mode

Essentially, hijacking the array clone set by the array.prototype.map() function. Thanks for the challenge! 🙌🏼

Collapse
 
aelmosalamy profile image
Adham Elmosalamy

Wow, I find it rather interesting how you found me here from reddit 😂
Anyways, brilliant solution, took me some time to understand, glad you liked it, keep it up!