Skip to content

Instantly share code, notes, and snippets.

@seisvelas
Forked from pancho508/fib.js
Last active November 23, 2019 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seisvelas/b3e01989062b3ce7f11e673d8f79afd4 to your computer and use it in GitHub Desktop.
Save seisvelas/b3e01989062b3ce7f11e673d8f79afd4 to your computer and use it in GitHub Desktop.
I wrote these at Pancho's to show him how ridiculously versatile .reduce() can be.
const range = (n) => [...Array(n).keys()];
const fib = (n) => range(n).slice(1).reduce(
(acc, value) => {
console.log([acc[1], acc[0]+acc[1]]);
return [acc[1], acc[0]+acc[1]]
}
, [0, 1])[0]
fib(5)
const mexicanWave = (str) =>
str.split("").reduce( (acc, value, index, array) => {
let wavedStr = array.concat();
wavedStr[index] = wavedStr[index].toUpperCase()
return acc.concat(wavedStr.join(""))
}, [])
mexicanWave("lol")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment