Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement