Last active
December 27, 2021 07:09
-
-
Save brycetshaw/e33bcd2cf7992f1fb9fa53988bff37f0 to your computer and use it in GitHub Desktop.
Solves the classic FizzBuzz problem in an aggressively functional programming style.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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