Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. type Factor = {
  2. n: number,
  3. word: string
  4. };
  5.  
  6. function createConverter(factors: readonly Factor[]): (n: number) => string {
  7. return (n: number): string => {
  8. const converted = factors.map(f => n % f.n == 0 ? f.word : '').join('');
  9. return converted || `${n}`;
  10. };
  11. }
  12.  
  13. const fizzbuzz = createConverter([
  14. {n: 3, word: 'Fizz'},
  15. {n: 5, word: 'Buzz'}
  16. ]);
  17. [9, 35, 15, 8].forEach(n => console.log(fizzbuzz(n)));
  18.  
  19. const raindrops = createConverter([
  20. {n: 3, word: 'Pling'},
  21. {n: 5, word: 'Plang'},
  22. {n: 7, word: 'Plong'}
  23. ]);
  24. [9, 10, 14, 15, 21, 35, 52, 105].forEach(n => console.log(raindrops(n)));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement