Last active
August 12, 2019 08:05
-
-
Save gpiancastelli/bf0e46385a997c700146e26e8aa0f365 to your computer and use it in GitHub Desktop.
A more general FizzBuzz solution that also works for Raindrops
This file contains 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
type Factor = { | |
n: number, | |
word: string | |
}; | |
function createConverter(factors: readonly Factor[]): (n: number) => string { | |
return (n: number): string => { | |
const converted = factors.map(f => n % f.n == 0 ? f.word : '').join(''); | |
return converted || `${n}`; | |
}; | |
} | |
const fizzbuzz = createConverter([ | |
{n: 3, word: 'Fizz'}, | |
{n: 5, word: 'Buzz'} | |
]); | |
[9, 35, 15, 8].forEach(n => console.log(fizzbuzz(n))); | |
const raindrops = createConverter([ | |
{n: 3, word: 'Pling'}, | |
{n: 5, word: 'Plang'}, | |
{n: 7, word: 'Plong'} | |
]); | |
[9, 10, 14, 15, 21, 35, 52, 105].forEach(n => console.log(raindrops(n))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment