Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

christiantakle commented Nov 8, 2017

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
You can’t perform that action at this time.