Skip to content

Instantly share code, notes, and snippets.

@selfsigned
Created May 27, 2024 16:04
Show Gist options
  • Save selfsigned/31a760e5fe4cdbb7274359f5a77609d6 to your computer and use it in GitHub Desktop.
Save selfsigned/31a760e5fe4cdbb7274359f5a77609d6 to your computer and use it in GitHub Desktop.
Functional FizzBuzz
// Logic
type FizzBuzzOutput = (x: number) => string
const Fizz : FizzBuzzOutput = x => (x % 3 === 0) ? "Fizz" : ""
const Buzz : FizzBuzzOutput = x => (x % 5 === 0) ? "Buzz" : ""
const FizzBuzzPrint : FizzBuzzOutput = x => Fizz(x) + Buzz(x)
type FizzBuzz = (x: number) => Array<string>
const FizzBuzz : FizzBuzz = x => x < 1 ? [] : [...FizzBuzz(x-1), FizzBuzzPrint(x)]
// Execution
console.log(FizzBuzz(100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment