Skip to content

Instantly share code, notes, and snippets.

@brycetshaw
Last active December 27, 2021 07:09
Show Gist options
  • Save brycetshaw/e33bcd2cf7992f1fb9fa53988bff37f0 to your computer and use it in GitHub Desktop.
Save brycetshaw/e33bcd2cf7992f1fb9fa53988bff37f0 to your computer and use it in GitHub Desktop.
Solves the classic FizzBuzz problem in an aggressively functional programming style.
function arrayOfNumbers(n) {
return Array.from(Array(n).keys())
.map((x) => ++x)
}
function fizzBuzz(n) {
const mutators = [
{key: 3, val:"Fizz"},
{key:5, val: "Buzz"}
];
return arrayOfNumbers(n)
.map((x) => {
const words = mutators
.filter(({key}) => x % key === 0)
.map(({val}) => val);
return words.length > 0
? words.join('')
: `${x}`;
}).join("\n");
};
console.log(fizzBuzz(15));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment