Skip to content

Instantly share code, notes, and snippets.

@christiantakle
Created November 8, 2017 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiantakle/2a154ff17515bb1a8055ac58288e3408 to your computer and use it in GitHub Desktop.
Save christiantakle/2a154ff17515bb1a8055ac58288e3408 to your computer and use it in GitHub Desktop.
Different examples of fizzbuzz in javascript
const
whenDivisibleBy = (x, t, f) => n => (n % x == 0 ? t : f),
fizz = whenDivisibleBy(3, 'Fizz', ''),
buzz = whenDivisibleBy(5, 'Buzz', ''),
fizzBuzz = n => fizz(n) + buzz(n) || String(n);
//Array(...Array(100)).map((_,x) => fizzBuzz(x+1))
@christiantakle
Copy link
Author

const fizzBuzz = n => {
  // :: Int -> String
  const
    fizz = n % 3 == 0 ? 'Fizz' : '',
    buzz = n % 5 == 0 ? 'Buzz' : '';

  return fizzBuzz.length ? fizz + buzz : String(n);
};

const
  divisibleBy = (x, s) => n => (n % x == 0 ? s : ''), // :: (Int,Sting) -> Int -> String
  fizz = divisibleBy(3, 'Fizz'),                      // :: Int -> String
  buzz = divisibleBy(5, 'Buzz'),                      // :: Int -> String
  fizzBuzz = n => fizz(n) + buzz(n) || String(n);     // :: Int -> String

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment